]>
vaikene.ee Git - evaf/blob - src/apps/FileFinder/GUI/gui.cpp
a849da970d2e2cf4da6da630ee2d39dfbc0848b6
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 FileFinder::GUI::Module::Module()
83 , mOpenDirectoryAction(0)
93 setObjectName(QString("%1.Module").arg(VER_MODULE_NAME_STR
));
95 EVAF_INFO("%s created", qPrintable(objectName()));
98 FileFinder::GUI::Module::~Module()
100 EVAF_INFO("%s destroyed", qPrintable(objectName()));
103 bool FileFinder::GUI::Module::init(QString
const & args
)
107 // Get the iFileFinder interface
108 EVAF_TEST_X((mFinder
= evafQueryInterface
<FileFinder::iFileFinder
>("iFileFinder")), "No iFileFinder interface");
109 connect(mFinder
, SIGNAL(found(QString
,QString
)), this, SLOT(found(QString
,QString
)));
110 connect(mFinder
, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
112 // Get the main window interface and fill it with widgets
113 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
114 EVAF_TEST_X(win
, "No iSdiWindow interface");
116 // Create the main widget for this window
117 wMain
= new Internal::MainWidget
;
118 connect(wMain
, SIGNAL(quit()), qApp
, SLOT(quit()));
119 win
->addWidget(wMain
);
121 // Create actions for the window and widgets on it
124 // Create all the other widgets
127 // Load stored searches
132 EVAF_INFO("%s initialized", qPrintable(objectName()));
137 void FileFinder::GUI::Module::done()
142 * Widgets are deleted by the SdiWindow module. We use wMain to track calls to done() without
150 EVAF_INFO("%s finalized", qPrintable(objectName()));
153 void FileFinder::GUI::Module::saveHistory()
155 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
158 for (int i
= 0; i
< wDirectory
->count(); ++i
)
159 t
.append(wDirectory
->itemText(i
));
160 settings
.setValue("FileFinder/Directories", t
);
163 for (int i
= 0; i
< wIncludeNames
->count(); ++i
)
164 t
.append(wIncludeNames
->itemText(i
));
165 settings
.setValue("FileFinder/IncludeNames", t
);
168 for (int i
= 0; i
< wExcludeNames
->count(); ++i
)
169 t
.append(wExcludeNames
->itemText(i
));
170 settings
.setValue("FileFinder/ExcludeNames", t
);
173 for (int i
= 0; i
< wIncludeContent
->count(); ++i
)
174 t
.append(wIncludeContent
->itemText(i
));
175 settings
.setValue("FileFinder/IncludeContent", t
);
178 for (int i
= 0; i
< wExcludeContent
->count(); ++i
)
179 t
.append(wExcludeContent
->itemText(i
));
180 settings
.setValue("FileFinder/ExcludeContent", t
);
184 void FileFinder::GUI::Module::loadHistory()
186 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
188 wDirectory
->addItems(settings
.value("FileFinder/Directories").toStringList());
189 wDirectory
->setEditText(QDir::currentPath());
191 wIncludeNames
->addItems(settings
.value("FileFinder/IncludeNames").toStringList());
192 wIncludeNames
->setEditText("");
194 wExcludeNames
->addItems(settings
.value("FileFinder/ExcludeNames").toStringList());
195 wExcludeNames
->setEditText("");
197 wIncludeContent
->addItems(settings
.value("FileFinder/IncludeContent").toStringList());
198 wIncludeContent
->setEditText("");
200 wExcludeContent
->addItems(settings
.value("FileFinder/ExcludeContent").toStringList());
201 wExcludeContent
->setEditText("");
204 void FileFinder::GUI::Module::createWidgets()
206 QVBoxLayout
* vbox
= new QVBoxLayout
;
207 wMain
->setLayout(vbox
);
209 QHBoxLayout
* hbox
= new QHBoxLayout
;
210 vbox
->addLayout(hbox
);
212 QLabel
* l
= new QLabel(tr("&Directory:"));
215 wDirectory
= new QComboBox
;
216 wDirectory
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
217 wDirectory
->setEditable(true);
218 wDirectory
->setInsertPolicy(QComboBox::InsertAtTop
);
219 l
->setBuddy(wDirectory
);
220 hbox
->addWidget(wDirectory
);
222 QPushButton
* btn
= new QPushButton(tr("&Browse"));
223 connect(btn
, SIGNAL(clicked()), this, SLOT(browseDirectory()));
224 hbox
->addWidget(btn
);
226 wRecursive
= new QCheckBox(tr("&Recursive"));
227 wRecursive
->setChecked(true);
228 hbox
->addWidget(wRecursive
);
230 QGridLayout
* gr
= new QGridLayout
;
233 gr
->setColumnStretch(1, 1);
234 gr
->setColumnStretch(3, 1);
236 l
= new QLabel(tr("&File filter:"));
237 gr
->addWidget(l
, 0, 0);
239 wIncludeNames
= new QComboBox
;
240 wIncludeNames
->setEditable(true);
241 wIncludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
242 l
->setBuddy(wIncludeNames
);
243 gr
->addWidget(wIncludeNames
, 0, 1);
245 l
= new QLabel(tr("¬:"));
246 gr
->addWidget(l
, 0, 2);
248 wExcludeNames
= new QComboBox
;
249 wExcludeNames
->setEditable(true);
250 wExcludeNames
->setInsertPolicy(QComboBox::InsertAtTop
);
251 l
->setBuddy(wExcludeNames
);
252 gr
->addWidget(wExcludeNames
);
254 l
= new QLabel(tr("C&ontains:"));
255 gr
->addWidget(l
, 1, 0);
257 wIncludeContent
= new QComboBox
;
258 wIncludeContent
->setEditable(true);
259 wIncludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
260 l
->setBuddy(wIncludeContent
);
261 gr
->addWidget(wIncludeContent
, 1, 1);
263 l
= new QLabel(tr("no&t:"));
264 gr
->addWidget(l
, 1, 2);
266 wExcludeContent
= new QComboBox
;
267 wExcludeContent
->setEditable(true);
268 wExcludeContent
->setInsertPolicy(QComboBox::InsertAtTop
);
269 l
->setBuddy(wExcludeContent
);
270 gr
->addWidget(wExcludeContent
, 1, 3);
272 wResults
= new QListWidget
;
273 wResults
->setContextMenuPolicy(Qt::ActionsContextMenu
);
274 wResults
->addAction(mOpenFileAction
);
275 wResults
->addAction(mOpenDirectoryAction
);
276 connect(wResults
, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
277 connect(wResults
, SIGNAL(doubleClicked(QModelIndex
)), this, SLOT(openFile(QModelIndex
)));
278 vbox
->addWidget(wResults
);
280 hbox
= new QHBoxLayout
;
281 vbox
->addLayout(hbox
);
284 wFind
= new QPushButton(tr("&Search"));
285 wFind
->setDefault(true);
286 connect(wFind
, SIGNAL(clicked()), this, SLOT(find()));
287 hbox
->addWidget(wFind
);
289 btn
= new QPushButton(tr("&Close"));
290 connect(btn
, SIGNAL(clicked()), qApp
, SLOT(quit()));
291 hbox
->addWidget(btn
);
294 void FileFinder::GUI::Module::createActions()
296 QAction
* a
= new QAction(wMain
);
297 a
->setShortcuts(QKeySequence::Quit
);
298 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
301 mOpenFileAction
= new QAction(tr("&Open"), wMain
);
302 mOpenFileAction
->setEnabled(false);
303 connect(mOpenFileAction
, SIGNAL(triggered()), this, SLOT(openFile()));
305 mOpenDirectoryAction
= new QAction(tr("Open &location"), wMain
);
306 mOpenDirectoryAction
->setEnabled(false);
307 connect(mOpenDirectoryAction
, SIGNAL(triggered()), this, SLOT(openDirectory()));
310 void FileFinder::GUI::Module::browseDirectory()
312 QString s
= QFileDialog::getExistingDirectory(wMain
, tr("Select the directory"), wDirectory
->currentText());
314 wDirectory
->setEditText(s
);
317 void FileFinder::GUI::Module::find()
321 if (mFinder
->busy()) {
327 mFinder
->search(wDirectory
->currentText(),
328 wRecursive
->isChecked(),
330 wIncludeNames
->currentText(),
331 wExcludeNames
->currentText(),
332 wIncludeContent
->currentText(),
333 wExcludeContent
->currentText()
337 wFind
->setText(tr("&Stop"));
341 void FileFinder::GUI::Module::found(QString
const & file
, QString
const & dir
)
343 QString result
= dir
;
344 if (!result
.endsWith(QChar('/')))
345 result
.append(QChar('/'));
347 wResults
->addItem(result
);
350 void FileFinder::GUI::Module::finished(bool canceled
)
354 wFind
->setText(tr("&Search"));
357 void FileFinder::GUI::Module::currentRowChanged(int currentRow
)
359 mOpenFileAction
->setEnabled(currentRow
>= 0);
360 mOpenDirectoryAction
->setEnabled(currentRow
>= 0);
363 void FileFinder::GUI::Module::openFile(QModelIndex
const & index
)
366 if (wResults
->currentItem())
367 QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(wResults
->currentItem()->text())));
370 void FileFinder::GUI::Module::openDirectory()
372 if (wResults
->currentItem()) {
373 QFileInfo
fi(wResults
->currentItem()->text());
374 QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(fi
.path())));