]>
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)
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
->addAction(mOpenFileAction
);
285 wResults
->addAction(mOpenDirectoryAction
);
286 connect(wResults
, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
287 connect(wResults
, SIGNAL(doubleClicked(QModelIndex
)), this, SLOT(openFile(QModelIndex
)));
288 vbox
->addWidget(wResults
);
290 hbox
= new QHBoxLayout
;
291 vbox
->addLayout(hbox
);
294 wFind
= new QPushButton(tr("&Search"));
295 wFind
->setDefault(true);
296 connect(wFind
, SIGNAL(clicked()), this, SLOT(find()));
297 hbox
->addWidget(wFind
);
299 QPushButton
* btn
= new QPushButton(tr("&Close"));
300 connect(btn
, SIGNAL(clicked()), qApp
, SLOT(quit()));
301 hbox
->addWidget(btn
);
304 void FileFinder::GUI::Module::createActions()
306 QAction
* a
= new QAction(wMain
);
307 a
->setShortcuts(QKeySequence::Quit
);
308 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
311 mOpenFileAction
= new QAction(tr("&Open"), wMain
);
312 mOpenFileAction
->setEnabled(false);
313 connect(mOpenFileAction
, SIGNAL(triggered()), this, SLOT(openFile()));
315 mOpenDirectoryAction
= new QAction(tr("Open &location"), wMain
);
316 mOpenDirectoryAction
->setEnabled(false);
317 connect(mOpenDirectoryAction
, SIGNAL(triggered()), this, SLOT(openDirectory()));
320 void FileFinder::GUI::Module::browseDirectory()
322 QString s
= QFileDialog::getExistingDirectory(wMain
, tr("Select the directory"), wDirectory
->currentText());
324 wDirectory
->setEditText(s
);
327 void FileFinder::GUI::Module::find()
331 if (mFinder
->busy()) {
337 if (wDirectory
->findText(wDirectory
->currentText()) == -1)
338 wDirectory
->insertItem(0, wDirectory
->currentText());
339 if (wIncludeNames
->findText(wIncludeNames
->currentText()) == -1)
340 wIncludeNames
->insertItem(0, wIncludeNames
->currentText());
341 if (wExcludeNames
->findText(wExcludeNames
->currentText()) == -1)
342 wExcludeNames
->insertItem(0, wExcludeNames
->currentText());
343 if (wIncludeContent
->findText(wIncludeContent
->currentText()) == -1)
344 wIncludeContent
->insertItem(0, wIncludeContent
->currentText());
345 if (wExcludeContent
->findText(wExcludeContent
->currentText()) == -1)
346 wExcludeContent
->insertItem(0, wExcludeContent
->currentText());
348 // Disable input fields
349 wDirectory
->setEnabled(false);
350 wBrowse
->setEnabled(false);
351 wRecursive
->setEnabled(false);
352 wIncludeNames
->setEnabled(false);
353 wExcludeNames
->setEnabled(false);
354 wIncludeContent
->setEnabled(false);
355 wExcludeContent
->setEnabled(false);
357 mFinder
->search(wDirectory
->currentText(),
358 wRecursive
->isChecked(),
360 wIncludeNames
->currentText(),
361 wExcludeNames
->currentText(),
362 wIncludeContent
->currentText(),
363 wExcludeContent
->currentText()
367 wFind
->setText(tr("&Stop"));
371 void FileFinder::GUI::Module::found(QString
const & file
, QString
const & dir
)
373 QString result
= dir
;
374 if (!result
.endsWith(QChar('/')))
375 result
.append(QChar('/'));
377 wResults
->addItem(result
);
380 void FileFinder::GUI::Module::finished(bool canceled
)
384 // Enable input fields
385 wDirectory
->setEnabled(true);
386 wBrowse
->setEnabled(true);
387 wRecursive
->setEnabled(true);
388 wIncludeNames
->setEnabled(true);
389 wExcludeNames
->setEnabled(true);
390 wIncludeContent
->setEnabled(true);
391 wExcludeContent
->setEnabled(true);
393 wFind
->setText(tr("&Search"));
396 void FileFinder::GUI::Module::currentRowChanged(int currentRow
)
398 mOpenFileAction
->setEnabled(currentRow
>= 0);
399 mOpenDirectoryAction
->setEnabled(currentRow
>= 0);
402 void FileFinder::GUI::Module::openFile(QModelIndex
const & index
)
405 if (wResults
->currentItem())
406 QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(wResults
->currentItem()->text())));
409 void FileFinder::GUI::Module::openDirectory()
411 if (wResults
->currentItem()) {
412 QFileInfo
fi(wResults
->currentItem()->text());
413 QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(fi
.path())));