]>
vaikene.ee Git - evaf/blob - src/apps/FileFinder/GUI/gui.cpp
2 * @file FileFinder/GUI/gui.cpp
3 * @brief GUI for the FileFinder application
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
23 #include "Engine/iFileFinder"
25 #include <Common/Globals>
26 #include <Common/iLogger>
27 #include <Common/iRegistry>
28 #include <Common/iApp>
29 #include <SdiWindow/iSdiWindow>
33 VER_EXPORT_VERSION_INFO()
34 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, eVaf::FileFinder::GUI::Module
)
39 //-------------------------------------------------------------------
41 void FileFinder::GUI::Internal::MainWidget::keyPressEvent(QKeyEvent
* e
)
43 if (!e
->modifiers() || ((e
->modifiers() & Qt::KeypadModifier
) && e
->key() == Qt::Key_Enter
)) {
46 case Qt::Key_Return
: {
47 QList
<QPushButton
*> buttons
= qFindChildren
<QPushButton
*>(this);
48 foreach (QPushButton
* btn
, buttons
) {
49 if (btn
->isDefault() && btn
->isVisible()) {
76 //-------------------------------------------------------------------
78 int const FileFinder::GUI::Module::MaxHistoryItems
= 20;
80 FileFinder::GUI::Module::Module()
85 , mOpenDirectoryAction(0)
87 , mCopyAllNamesAction(0)
97 setObjectName(QString("%1.Module").arg(VER_MODULE_NAME_STR
));
99 EVAF_INFO("%s created", qPrintable(objectName()));
102 FileFinder::GUI::Module::~Module()
104 EVAF_INFO("%s destroyed", qPrintable(objectName()));
107 bool FileFinder::GUI::Module::init(QString
const & args
)
111 // Get the iFileFinder interface
112 EVAF_TEST_X((mFinder
= evafQueryInterface
<FileFinder::iFileFinder
>("iFileFinder")), "No iFileFinder interface");
113 connect(mFinder
, SIGNAL(found(QString
,QString
)), this, SLOT(found(QString
,QString
)));
114 connect(mFinder
, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
116 // Get the main window interface and fill it with widgets
117 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
118 EVAF_TEST_X(win
, "No iSdiWindow interface");
120 // Create the main widget for this window
121 wMain
= new Internal::MainWidget
;
122 connect(wMain
, SIGNAL(quit()), qApp
, SLOT(quit()));
123 win
->addPanel("FileFinder", wMain
);
125 // Create actions for the window and widgets on it
128 // Create all the other widgets
131 // Load stored searches
136 EVAF_INFO("%s initialized", qPrintable(objectName()));
141 void FileFinder::GUI::Module::done()
148 * Widgets are deleted by the SdiWindow module. We use wMain to track calls to done() without
156 EVAF_INFO("%s finalized", qPrintable(objectName()));
159 void FileFinder::GUI::Module::saveHistory()
161 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
164 for (int i
= 0; i
< wDirectory
->count() && i
< MaxHistoryItems
; ++i
)
165 t
.append(wDirectory
->itemText(i
));
166 settings
.setValue("FileFinder/Directories", t
);
169 for (int i
= 0; i
< wIncludeNames
->count() && i
< MaxHistoryItems
; ++i
)
170 t
.append(wIncludeNames
->itemText(i
));
171 settings
.setValue("FileFinder/IncludeNames", t
);
174 for (int i
= 0; i
< wExcludeNames
->count() && i
< MaxHistoryItems
; ++i
)
175 t
.append(wExcludeNames
->itemText(i
));
176 settings
.setValue("FileFinder/ExcludeNames", t
);
179 for (int i
= 0; i
< wIncludeContent
->count() && i
< MaxHistoryItems
; ++i
)
180 t
.append(wIncludeContent
->itemText(i
));
181 settings
.setValue("FileFinder/IncludeContent", t
);
184 for (int i
= 0; i
< wExcludeContent
->count() && i
< MaxHistoryItems
; ++i
)
185 t
.append(wExcludeContent
->itemText(i
));
186 settings
.setValue("FileFinder/ExcludeContent", t
);
190 void FileFinder::GUI::Module::loadHistory()
192 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
194 wDirectory
->addItems(settings
.value("FileFinder/Directories").toStringList());
195 wDirectory
->setEditText(QDir::currentPath());
196 mDirModel
->setRootPath(QDir::currentPath());
198 wIncludeNames
->addItems(settings
.value("FileFinder/IncludeNames").toStringList());
199 wIncludeNames
->setEditText("");
201 wExcludeNames
->addItems(settings
.value("FileFinder/ExcludeNames").toStringList());
202 wExcludeNames
->setEditText("");
204 wIncludeContent
->addItems(settings
.value("FileFinder/IncludeContent").toStringList());
205 wIncludeContent
->setEditText("");
207 wExcludeContent
->addItems(settings
.value("FileFinder/ExcludeContent").toStringList());
208 wExcludeContent
->setEditText("");
211 void FileFinder::GUI::Module::createWidgets()
213 QVBoxLayout
* vbox
= new QVBoxLayout
;
214 wMain
->setLayout(vbox
);
216 QHBoxLayout
* hbox
= new QHBoxLayout
;
217 vbox
->addLayout(hbox
);
219 QLabel
* l
= new QLabel(tr("&Directory:"));
222 wDirectory
= new QComboBox
;
223 wDirectory
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
224 wDirectory
->setEditable(true);
225 wDirectory
->setInsertPolicy(QComboBox::InsertAtTop
);
226 QCompleter
* completer
= new QCompleter(wMain
);
227 mDirModel
= new QFileSystemModel(wMain
);
228 mDirModel
->setFilter(QDir::AllDirs
| QDir::Dirs
| QDir::NoDotAndDotDot
);
229 completer
->setModel(mDirModel
);
230 wDirectory
->setCompleter(completer
);
231 l
->setBuddy(wDirectory
);
232 hbox
->addWidget(wDirectory
);
234 wBrowse
= new QPushButton(tr("&Browse"));
235 connect(wBrowse
, SIGNAL(clicked()), this, SLOT(browseDirectory()));
236 hbox
->addWidget(wBrowse
);
238 wRecursive
= new QCheckBox(tr("&Recursive"));
239 wRecursive
->setChecked(true);
240 hbox
->addWidget(wRecursive
);
242 QGridLayout
* gr
= new QGridLayout
;
245 gr
->setColumnStretch(1, 1);
246 gr
->setColumnStretch(3, 1);
248 l
= new QLabel(tr("&File filter:"));
249 gr
->addWidget(l
, 0, 0);
251 wIncludeNames
= new QComboBox
;
252 wIncludeNames
->setEditable(true);
253 wIncludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
254 l
->setBuddy(wIncludeNames
);
255 gr
->addWidget(wIncludeNames
, 0, 1);
257 l
= new QLabel(tr("¬:"));
258 gr
->addWidget(l
, 0, 2);
260 wExcludeNames
= new QComboBox
;
261 wExcludeNames
->setEditable(true);
262 wExcludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
263 l
->setBuddy(wExcludeNames
);
264 gr
->addWidget(wExcludeNames
);
266 l
= new QLabel(tr("C&ontains:"));
267 gr
->addWidget(l
, 1, 0);
269 wIncludeContent
= new QComboBox
;
270 wIncludeContent
->setEditable(true);
271 wIncludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
272 l
->setBuddy(wIncludeContent
);
273 gr
->addWidget(wIncludeContent
, 1, 1);
275 l
= new QLabel(tr("no&t:"));
276 gr
->addWidget(l
, 1, 2);
278 wExcludeContent
= new QComboBox
;
279 wExcludeContent
->setEditable(true);
280 wExcludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
281 l
->setBuddy(wExcludeContent
);
282 gr
->addWidget(wExcludeContent
, 1, 3);
284 wResults
= new QListWidget
;
285 wResults
->setContextMenuPolicy(Qt::ActionsContextMenu
);
286 wResults
->addActions(QList
<QAction
*>() << mOpenFileAction
<< mOpenDirectoryAction
<< mCopyNameAction
<< mCopyAllNamesAction
);
287 connect(wResults
, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
288 connect(wResults
, SIGNAL(doubleClicked(QModelIndex
)), this, SLOT(openFile(QModelIndex
)));
289 vbox
->addWidget(wResults
);
291 hbox
= new QHBoxLayout
;
292 vbox
->addLayout(hbox
);
295 wFind
= new QPushButton(tr("&Search"));
296 wFind
->setDefault(true);
297 connect(wFind
, SIGNAL(clicked()), this, SLOT(find()));
298 hbox
->addWidget(wFind
);
300 QPushButton
* btn
= new QPushButton(tr("&Close"));
301 connect(btn
, SIGNAL(clicked()), qApp
, SLOT(quit()));
302 hbox
->addWidget(btn
);
305 void FileFinder::GUI::Module::createActions()
307 QAction
* a
= new QAction(wMain
);
308 a
->setShortcuts(QKeySequence::Quit
);
309 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
312 mOpenFileAction
= new QAction(tr("&Open"), wMain
);
313 mOpenFileAction
->setEnabled(false);
314 connect(mOpenFileAction
, SIGNAL(triggered()), this, SLOT(openFile()));
316 mOpenDirectoryAction
= new QAction(tr("Open &location"), wMain
);
317 mOpenDirectoryAction
->setEnabled(false);
318 connect(mOpenDirectoryAction
, SIGNAL(triggered()), this, SLOT(openDirectory()));
320 mCopyNameAction
= new QAction(tr("&Copy name"), wMain
);
321 mCopyNameAction
->setEnabled(false);
322 connect(mCopyNameAction
, SIGNAL(triggered()), this, SLOT(copyName()));
324 mCopyAllNamesAction
= new QAction(tr("Copy &all names"), wMain
);
325 connect(mCopyAllNamesAction
, SIGNAL(triggered()), this, SLOT(copyAllNames()));
329 void FileFinder::GUI::Module::browseDirectory()
331 QString s
= QFileDialog::getExistingDirectory(wMain
, tr("Select the directory"), wDirectory
->currentText());
333 wDirectory
->setEditText(s
);
336 void FileFinder::GUI::Module::find()
340 if (mFinder
->busy()) {
346 if (wDirectory
->findText(wDirectory
->currentText()) == -1)
347 wDirectory
->insertItem(0, wDirectory
->currentText());
348 if (wIncludeNames
->findText(wIncludeNames
->currentText()) == -1)
349 wIncludeNames
->insertItem(0, wIncludeNames
->currentText());
350 if (wExcludeNames
->findText(wExcludeNames
->currentText()) == -1)
351 wExcludeNames
->insertItem(0, wExcludeNames
->currentText());
352 if (wIncludeContent
->findText(wIncludeContent
->currentText()) == -1)
353 wIncludeContent
->insertItem(0, wIncludeContent
->currentText());
354 if (wExcludeContent
->findText(wExcludeContent
->currentText()) == -1)
355 wExcludeContent
->insertItem(0, wExcludeContent
->currentText());
357 // Disable input fields
358 wDirectory
->setEnabled(false);
359 wBrowse
->setEnabled(false);
360 wRecursive
->setEnabled(false);
361 wIncludeNames
->setEnabled(false);
362 wExcludeNames
->setEnabled(false);
363 wIncludeContent
->setEnabled(false);
364 wExcludeContent
->setEnabled(false);
366 mFinder
->search(wDirectory
->currentText(),
367 wRecursive
->isChecked(),
369 wIncludeNames
->currentText(),
370 wExcludeNames
->currentText(),
371 wIncludeContent
->currentText(),
372 wExcludeContent
->currentText()
376 wFind
->setText(tr("&Stop"));
380 void FileFinder::GUI::Module::found(QString
const & file
, QString
const & dir
)
382 QString result
= dir
;
383 if (!result
.endsWith(QChar('/')))
384 result
.append(QChar('/'));
386 wResults
->addItem(result
);
389 void FileFinder::GUI::Module::finished(bool canceled
)
393 // Enable input fields
394 wDirectory
->setEnabled(true);
395 wBrowse
->setEnabled(true);
396 wRecursive
->setEnabled(true);
397 wIncludeNames
->setEnabled(true);
398 wExcludeNames
->setEnabled(true);
399 wIncludeContent
->setEnabled(true);
400 wExcludeContent
->setEnabled(true);
402 wFind
->setText(tr("&Search"));
405 void FileFinder::GUI::Module::currentRowChanged(int currentRow
)
407 mOpenFileAction
->setEnabled(currentRow
>= 0);
408 mOpenDirectoryAction
->setEnabled(currentRow
>= 0);
409 mCopyNameAction
->setEnabled(currentRow
>= 0);
412 void FileFinder::GUI::Module::openFile(QModelIndex
const & index
)
415 if (wResults
->currentItem()) {
416 QString t
= wResults
->currentItem()->text();
417 t
.replace("?", "\0453f");
418 QDesktopServices::openUrl(QUrl::fromEncoded(QString("file:///%1").arg(t
).toUtf8()));
422 void FileFinder::GUI::Module::openDirectory()
424 if (wResults
->currentItem()) {
425 QString t
= wResults
->currentItem()->text();
426 t
.replace("?", "\0453f");
428 QDesktopServices::openUrl(QUrl::fromEncoded(QString("file:///%1").arg(fi
.path()).toUtf8()));
432 void FileFinder::GUI::Module::copyName()
434 if (wResults
->currentItem()) {
435 QClipboard
* cb
= QApplication::clipboard();
437 cb
->setText(wResults
->currentItem()->text());
441 void FileFinder::GUI::Module::copyAllNames()
444 static char const * const EOLN
= "\r\n";
446 static char const * const EOLN
= "\n";
450 for (int i
= 0; i
< wResults
->count(); ++i
)
451 t
.append(wResults
->item(i
)->text() + EOLN
);
453 QClipboard
* cb
= QApplication::clipboard();