[3e4acc]: kwave / KwaveApp.cpp Maximize Restore History

Download this file

KwaveApp.cpp    254 lines (209 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************
KwaveApp.cpp - The Kwave main application
-------------------
begin : Wed Feb 28 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QFile>
#include <QString>
#include <QMetaType>
#include <kcmdlineargs.h>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <ktoolinvocation.h>
#include "libkwave/ApplicationContext.h"
#include "libkwave/ClipBoard.h"
#include "libkwave/LabelList.h"
#include "libkwave/KwaveSampleArray.h"
#include "libkwave/MemoryManager.h"
#include "libkwave/Parser.h"
#include "libkwave/Sample.h"
#include "libkwave/SignalManager.h"
#include "libkwave/PluginManager.h"
#include "TopWidget.h"
#include "KwaveApp.h"
#include "KwaveSplash.h"
//***************************************************************************
KwaveApp::KwaveApp()
:KUniqueApplication(),
m_recent_files(),
m_topwidget_list()
{
qRegisterMetaType<Kwave::SampleArray>("Kwave::SampleArray");
qRegisterMetaType<LabelList>("LabelList");
qRegisterMetaType<sample_index_t>("sample_index_t");
qRegisterMetaType<Kwave::MetaDataList>("Kwave::MetaDataList");
// connect the clipboard
connect(QApplication::clipboard(), SIGNAL(changed(QClipboard::Mode)),
&(ClipBoard::instance()), SLOT(slotChanged(QClipboard::Mode)));
}
//***************************************************************************
int KwaveApp::newInstance()
{
static bool first_time = true;
if (first_time) {
first_time = false;
KwaveSplash::showMessage(i18n("Reading configuration..."));
readConfig();
// close when the last window closed
connect(this, SIGNAL(lastWindowClosed()), this, SLOT(quit()));
}
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
unsigned int argc = (args) ? args->count() : 0;
// only one parameter -> open with empty window
if (argc == 0) {
newWindow(KUrl(QString()));
} else {
// open a window for each file specified in the
// command line an load it
for (unsigned int i = 0; i < argc; i++) {
QString name = args->arg(i);
QFileInfo file(name);
newWindow(file.absoluteFilePath());
}
}
if (args) args->clear();
return 0;
}
//***************************************************************************
bool KwaveApp::isOK()
{
return (!m_topwidget_list.isEmpty());
}
//***************************************************************************
bool KwaveApp::executeCommand(const QString &command)
{
Parser parser(command);
if (parser.command() == "newwindow") {
if (parser.hasParams()) {
newWindow(KUrl(parser.params().at(0)));
} else {
newWindow(KUrl(QString()));
}
} else if (parser.command() == "help") {
KToolInvocation::invokeHelp();
} else {
return false;
}
return true;
}
//***************************************************************************
void KwaveApp::addRecentFile(const QString &newfile)
{
if (!newfile.length()) return;
// remove old entries if present
m_recent_files.removeAll(newfile);
// shorten the list down to 19 entries
while (m_recent_files.count() > 19)
m_recent_files.removeLast();
// insert the new entry at top
m_recent_files.prepend(newfile);
// save the list of recent files
saveRecentFiles();
// update all toplevel widgets
emit recentFilesChanged();
}
//***************************************************************************
bool KwaveApp::newWindow(const KUrl &url)
{
KwaveSplash::showMessage(i18n("Opening main window..."));
Kwave::ApplicationContext *context = new Kwave::ApplicationContext(*this);
if (!context) return false;
if (!context->init()) {
delete context;
return false;
}
TopWidget *new_top_widget = context->topWidget();
if (m_topwidget_list.isEmpty()) {
// the first widget is the main widget !
setTopWidget(new_top_widget); // sets geometry and other properties
setTopWidget(0); // that's enough, dont quit on close !
} else {
// create a new widget with the same geometry as
// the last created one
const QRect &geom = m_topwidget_list.last()->geometry();
// calling setGeometry(geom) would overlap :-(
new_top_widget->resize(geom.width(), geom.height());
}
m_topwidget_list.append(new_top_widget);
new_top_widget->show();
// inform the widget about changes in the list of recent files
connect(this, SIGNAL(recentFilesChanged()),
new_top_widget, SLOT(updateRecentFiles()));
if (!url.isEmpty()) {
KwaveSplash::showMessage(i18n("Loading file '%1'...",
url.prettyUrl()));
new_top_widget->loadFile(url);
}
KwaveSplash::showMessage(i18n("Startup done"));
return true;
}
//***************************************************************************
bool KwaveApp::closeWindow(TopWidget *todel)
{
Q_ASSERT(todel);
// save the configuration, including the list of recent files
saveConfig();
if (todel) m_topwidget_list.removeAll(todel);
// if list is empty -> no more windows there -> exit application
return (m_topwidget_list.isEmpty());
}
//***************************************************************************
void KwaveApp::saveRecentFiles()
{
KConfigGroup cfg = KGlobal::config()->group("Recent Files");
QString num;
for (int i = 0 ; i < m_recent_files.count(); i++) {
num.setNum(i);
cfg.writeEntry(num, m_recent_files[i]);
}
cfg.sync();
}
//***************************************************************************
void KwaveApp::saveConfig()
{
// save the list of recent files
saveRecentFiles();
}
//***************************************************************************
void KwaveApp::readConfig()
{
QString result;
QString key;
const KConfigGroup cfg = KGlobal::config()->group("Recent Files");
for (unsigned int i = 0 ; i < 20; i++) {
key = QString::number(i); // generate number
// read corresponding entry, which is stored in UTF-8
result = cfg.readEntry(key);
if (result.length()) {
QFile file(result);
//check if file exists and insert it if not already present
if (file.exists() && (m_recent_files.contains(result) == 0))
m_recent_files.append(result);
}
}
}
//***************************************************************************
KwaveApp::~KwaveApp()
{
saveConfig();
while (!m_topwidget_list.isEmpty()) {
TopWidget *todel = m_topwidget_list.takeLast();
if (todel) delete todel;
}
m_recent_files.clear();
}
//***************************************************************************
#include "KwaveApp.moc"
//***************************************************************************
//***************************************************************************