]>
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-2019 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.
22 #include "Engine/iFileFinder"
24 #include <Common/Globals>
25 #include <Common/iLogger>
26 #include <Common/iRegistry>
27 #include <Common/iApp>
28 #include <SdiWindow/iSdiWindow>
32 VER_EXPORT_VERSION_INFO()
37 //-------------------------------------------------------------------
39 void FileFinder::GUI::Internal::MainWidget::keyPressEvent(QKeyEvent
* e
)
41 if (!e
->modifiers() || ((e
->modifiers() & Qt::KeypadModifier
) && e
->key() == Qt::Key_Enter
)) {
44 case Qt::Key_Return
: {
45 QList
<QPushButton
*> buttons
= findChildren
<QPushButton
*>();
46 foreach (QPushButton
* btn
, buttons
) {
47 if (btn
->isDefault() && btn
->isVisible()) {
74 //-------------------------------------------------------------------
76 int const FileFinder::GUI::Module::MaxHistoryItems
= 20;
78 FileFinder::GUI::Module::Module()
82 , mOpenFileAction(nullptr)
83 , mOpenDirectoryAction(nullptr)
84 , mCopyNameAction(nullptr)
85 , mCopyAllNamesAction(nullptr)
89 , wIncludeNames(nullptr)
90 , wExcludeNames(nullptr)
91 , wIncludeContent(nullptr)
92 , wExcludeContent(nullptr)
95 setObjectName(QString("%1.Module").arg(VER_MODULE_NAME_STR
));
97 EVAF_INFO("%s created", qPrintable(objectName()));
100 FileFinder::GUI::Module::~Module()
102 EVAF_INFO("%s destroyed", qPrintable(objectName()));
105 bool FileFinder::GUI::Module::init(QString
const & args
)
109 // Get the iFileFinder interface
110 EVAF_TEST_X((mFinder
= evafQueryInterface
<FileFinder::iFileFinder
>("iFileFinder")), "No iFileFinder interface")
111 connect(mFinder
, SIGNAL(found(QString
,QString
)), this, SLOT(found(QString
,QString
)));
112 connect(mFinder
, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
114 // Get the main window interface and fill it with widgets
115 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
116 EVAF_TEST_X(win
, "No iSdiWindow interface")
118 // Create the main widget for this window
119 wMain
= new Internal::MainWidget
;
120 connect(wMain
, SIGNAL(quit()), qApp
, SLOT(quit()));
121 win
->addPanel("FileFinder", wMain
);
123 // Create actions for the window and widgets on it
126 // Create all the other widgets
129 // Load stored searches
134 EVAF_INFO("%s initialized", qPrintable(objectName()));
139 void FileFinder::GUI::Module::done()
146 * Widgets are deleted by the SdiWindow module. We use wMain to track calls to done() without
154 EVAF_INFO("%s finalized", qPrintable(objectName()));
157 void FileFinder::GUI::Module::saveHistory()
159 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
162 for (int i
= 0; i
< wDirectory
->count() && i
< MaxHistoryItems
; ++i
)
163 t
.append(wDirectory
->itemText(i
));
164 settings
.setValue("FileFinder/Directories", t
);
167 for (int i
= 0; i
< wIncludeNames
->count() && i
< MaxHistoryItems
; ++i
)
168 t
.append(wIncludeNames
->itemText(i
));
169 settings
.setValue("FileFinder/IncludeNames", t
);
172 for (int i
= 0; i
< wExcludeNames
->count() && i
< MaxHistoryItems
; ++i
)
173 t
.append(wExcludeNames
->itemText(i
));
174 settings
.setValue("FileFinder/ExcludeNames", t
);
177 for (int i
= 0; i
< wIncludeContent
->count() && i
< MaxHistoryItems
; ++i
)
178 t
.append(wIncludeContent
->itemText(i
));
179 settings
.setValue("FileFinder/IncludeContent", t
);
182 for (int i
= 0; i
< wExcludeContent
->count() && i
< MaxHistoryItems
; ++i
)
183 t
.append(wExcludeContent
->itemText(i
));
184 settings
.setValue("FileFinder/ExcludeContent", t
);
188 void FileFinder::GUI::Module::loadHistory()
190 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
192 wDirectory
->addItems(settings
.value("FileFinder/Directories").toStringList());
193 wDirectory
->setEditText(QDir::currentPath());
194 mDirModel
->setRootPath(QDir::currentPath());
196 wIncludeNames
->addItems(settings
.value("FileFinder/IncludeNames").toStringList());
197 wIncludeNames
->setEditText("");
199 wExcludeNames
->addItems(settings
.value("FileFinder/ExcludeNames").toStringList());
200 wExcludeNames
->setEditText("");
202 wIncludeContent
->addItems(settings
.value("FileFinder/IncludeContent").toStringList());
203 wIncludeContent
->setEditText("");
205 wExcludeContent
->addItems(settings
.value("FileFinder/ExcludeContent").toStringList());
206 wExcludeContent
->setEditText("");
209 void FileFinder::GUI::Module::createWidgets()
211 QVBoxLayout
* vbox
= new QVBoxLayout
;
212 wMain
->setLayout(vbox
);
214 QHBoxLayout
* hbox
= new QHBoxLayout
;
215 vbox
->addLayout(hbox
);
217 QLabel
* l
= new QLabel(tr("&Directory:"));
220 wDirectory
= new QComboBox
;
221 wDirectory
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
222 wDirectory
->setEditable(true);
223 wDirectory
->setInsertPolicy(QComboBox::InsertAtTop
);
224 QCompleter
* completer
= new QCompleter(wMain
);
225 mDirModel
= new QFileSystemModel(wMain
);
226 mDirModel
->setFilter(QDir::AllDirs
| QDir::Dirs
| QDir::NoDotAndDotDot
);
227 completer
->setModel(mDirModel
);
228 wDirectory
->setCompleter(completer
);
229 l
->setBuddy(wDirectory
);
230 hbox
->addWidget(wDirectory
);
232 wBrowse
= new QPushButton(tr("&Browse"));
233 connect(wBrowse
, SIGNAL(clicked()), this, SLOT(browseDirectory()));
234 hbox
->addWidget(wBrowse
);
236 wRecursive
= new QCheckBox(tr("&Recursive"));
237 wRecursive
->setChecked(true);
238 hbox
->addWidget(wRecursive
);
240 QGridLayout
* gr
= new QGridLayout
;
243 gr
->setColumnStretch(1, 1);
244 gr
->setColumnStretch(3, 1);
246 l
= new QLabel(tr("&File filter:"));
247 gr
->addWidget(l
, 0, 0);
249 wIncludeNames
= new QComboBox
;
250 wIncludeNames
->setEditable(true);
251 wIncludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
252 l
->setBuddy(wIncludeNames
);
253 gr
->addWidget(wIncludeNames
, 0, 1);
255 l
= new QLabel(tr("¬:"));
256 gr
->addWidget(l
, 0, 2);
258 wExcludeNames
= new QComboBox
;
259 wExcludeNames
->setEditable(true);
260 wExcludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
261 l
->setBuddy(wExcludeNames
);
262 gr
->addWidget(wExcludeNames
);
264 l
= new QLabel(tr("C&ontains:"));
265 gr
->addWidget(l
, 1, 0);
267 wIncludeContent
= new QComboBox
;
268 wIncludeContent
->setEditable(true);
269 wIncludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
270 l
->setBuddy(wIncludeContent
);
271 gr
->addWidget(wIncludeContent
, 1, 1);
273 l
= new QLabel(tr("no&t:"));
274 gr
->addWidget(l
, 1, 2);
276 wExcludeContent
= new QComboBox
;
277 wExcludeContent
->setEditable(true);
278 wExcludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
279 l
->setBuddy(wExcludeContent
);
280 gr
->addWidget(wExcludeContent
, 1, 3);
282 wResults
= new QListWidget
;
283 wResults
->setContextMenuPolicy(Qt::ActionsContextMenu
);
284 wResults
->addActions(QList
<QAction
*>() << mOpenFileAction
<< mOpenDirectoryAction
<< mCopyNameAction
<< mCopyAllNamesAction
);
285 connect(wResults
, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
286 connect(wResults
, SIGNAL(doubleClicked(QModelIndex
)), this, SLOT(openFile(QModelIndex
)));
287 vbox
->addWidget(wResults
);
289 hbox
= new QHBoxLayout
;
290 vbox
->addLayout(hbox
);
293 wFind
= new QPushButton(tr("&Search"));
294 wFind
->setDefault(true);
295 connect(wFind
, SIGNAL(clicked()), this, SLOT(find()));
296 hbox
->addWidget(wFind
);
298 QPushButton
* btn
= new QPushButton(tr("&Close"));
299 connect(btn
, SIGNAL(clicked()), qApp
, SLOT(quit()));
300 hbox
->addWidget(btn
);
303 void FileFinder::GUI::Module::createActions()
305 QAction
* a
= new QAction(wMain
);
306 a
->setShortcuts(QKeySequence::Quit
);
307 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
310 mOpenFileAction
= new QAction(tr("&Open"), wMain
);
311 mOpenFileAction
->setEnabled(false);
312 connect(mOpenFileAction
, SIGNAL(triggered()), this, SLOT(openFile()));
314 mOpenDirectoryAction
= new QAction(tr("Open &location"), wMain
);
315 mOpenDirectoryAction
->setEnabled(false);
316 connect(mOpenDirectoryAction
, SIGNAL(triggered()), this, SLOT(openDirectory()));
318 mCopyNameAction
= new QAction(tr("&Copy name"), wMain
);
319 mCopyNameAction
->setEnabled(false);
320 connect(mCopyNameAction
, SIGNAL(triggered()), this, SLOT(copyName()));
322 mCopyAllNamesAction
= new QAction(tr("Copy &all names"), wMain
);
323 connect(mCopyAllNamesAction
, SIGNAL(triggered()), this, SLOT(copyAllNames()));
327 void FileFinder::GUI::Module::browseDirectory()
329 QString s
= QFileDialog::getExistingDirectory(wMain
, tr("Select the directory"), wDirectory
->currentText());
331 wDirectory
->setEditText(s
);
334 void FileFinder::GUI::Module::find()
338 if (mFinder
->busy()) {
344 if (wDirectory
->findText(wDirectory
->currentText()) == -1)
345 wDirectory
->insertItem(0, wDirectory
->currentText());
346 if (wIncludeNames
->findText(wIncludeNames
->currentText()) == -1)
347 wIncludeNames
->insertItem(0, wIncludeNames
->currentText());
348 if (wExcludeNames
->findText(wExcludeNames
->currentText()) == -1)
349 wExcludeNames
->insertItem(0, wExcludeNames
->currentText());
350 if (wIncludeContent
->findText(wIncludeContent
->currentText()) == -1)
351 wIncludeContent
->insertItem(0, wIncludeContent
->currentText());
352 if (wExcludeContent
->findText(wExcludeContent
->currentText()) == -1)
353 wExcludeContent
->insertItem(0, wExcludeContent
->currentText());
355 // Disable input fields
356 wDirectory
->setEnabled(false);
357 wBrowse
->setEnabled(false);
358 wRecursive
->setEnabled(false);
359 wIncludeNames
->setEnabled(false);
360 wExcludeNames
->setEnabled(false);
361 wIncludeContent
->setEnabled(false);
362 wExcludeContent
->setEnabled(false);
364 mFinder
->search(wDirectory
->currentText(),
365 wRecursive
->isChecked(),
367 wIncludeNames
->currentText(),
368 wExcludeNames
->currentText(),
369 wIncludeContent
->currentText(),
370 wExcludeContent
->currentText()
374 wFind
->setText(tr("&Stop"));
378 void FileFinder::GUI::Module::found(QString
const & file
, QString
const & dir
)
380 QString result
= dir
;
381 if (!result
.endsWith(QChar('/')))
382 result
.append(QChar('/'));
384 wResults
->addItem(result
);
387 void FileFinder::GUI::Module::finished(bool canceled
)
391 // Enable input fields
392 wDirectory
->setEnabled(true);
393 wBrowse
->setEnabled(true);
394 wRecursive
->setEnabled(true);
395 wIncludeNames
->setEnabled(true);
396 wExcludeNames
->setEnabled(true);
397 wIncludeContent
->setEnabled(true);
398 wExcludeContent
->setEnabled(true);
400 wFind
->setText(tr("&Search"));
403 void FileFinder::GUI::Module::currentRowChanged(int currentRow
)
405 mOpenFileAction
->setEnabled(currentRow
>= 0);
406 mOpenDirectoryAction
->setEnabled(currentRow
>= 0);
407 mCopyNameAction
->setEnabled(currentRow
>= 0);
410 void FileFinder::GUI::Module::openFile(QModelIndex
const & index
)
413 if (wResults
->currentItem()) {
414 QString t
= wResults
->currentItem()->text();
415 t
.replace("?", "\0453f");
416 QDesktopServices::openUrl(QUrl::fromEncoded(QString("file:///%1").arg(t
).toUtf8()));
420 void FileFinder::GUI::Module::openDirectory()
422 if (wResults
->currentItem()) {
423 QString t
= wResults
->currentItem()->text();
424 t
.replace("?", "\0453f");
426 QDesktopServices::openUrl(QUrl::fromEncoded(QString("file:///%1").arg(fi
.path()).toUtf8()));
430 void FileFinder::GUI::Module::copyName()
432 if (wResults
->currentItem()) {
433 QClipboard
* cb
= QApplication::clipboard();
435 cb
->setText(wResults
->currentItem()->text());
439 void FileFinder::GUI::Module::copyAllNames()
442 static char const * const EOLN
= "\r\n";
444 static char const * const EOLN
= "\n";
448 for (int i
= 0; i
< wResults
->count(); ++i
)
449 t
.append(wResults
->item(i
)->text() + EOLN
);
451 QClipboard
* cb
= QApplication::clipboard();