-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
116 lines (95 loc) · 3.31 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "settingsdialog.h"
#include <QDir>
#include <QGraphicsPixmapItem>
#include <QSettings>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QStandardPaths>
Q_LOGGING_CATEGORY(EDIM, "edim")
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
_library(this),
_settingsDialog(new SettingsDialog(this))
{
setupUi(this);
treeViewLibrary->setModel(&_library);
treeViewLibrary->setRootIndex(_library.index(QFileInfo(_library.basePath().absolutePath())));
treeViewLibrary->hideColumn(1);
treeViewLibrary->hideColumn(2);
treeViewLibrary->hideColumn(3);
graphicsViewPreview->setScene(&_previewScene);
setupConnections();
readSettings();
}
void MainWindow::setupConnections()
{
// Actions
connect(actionQuit, &QAction::triggered, this, &QMainWindow::close);
connect(actionSettings, &QAction::triggered, _settingsDialog, &QDialog::show);
// UI elements
connect(actionImport, &QAction::triggered, this, &MainWindow::importDocument);
connect(treeViewLibrary, &QTreeView::clicked, this, &MainWindow::showDocument);
connect(lineEditSearch, &QLineEdit::textChanged, this, &MainWindow::searchDocument);
}
void MainWindow::closeEvent(QCloseEvent* event)
{
writeSettings();
QMainWindow::closeEvent(event);
}
void MainWindow::readSettings()
{
QSettings settings;
settings.beginGroup("MainWindow");
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("state").toByteArray());
settings.endGroup();
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.beginGroup("MainWindow");
settings.setValue("geometry", saveGeometry());
settings.setValue("state", saveState());
settings.endGroup();
}
void MainWindow::showSettings() const
{
_settingsDialog->show();
}
void MainWindow::importDocument()
{
QFileInfo document(_library.fileInfo(treeViewLibrary->currentIndex()));
if (!_library.contains(document)) {
_library.import(document);
} else {
// TODO Add an "Update entry instead?" dialog here
qCDebug(EDIM) << "Library already contains document" << document.absoluteFilePath();
}
}
void MainWindow::showDocument(const QModelIndex& index)
{
_previewScene.clear();
// TODO add sanity checks
QFileInfo document(_library.fileInfo(index));
_previewScene.addItem(new QGraphicsPixmapItem(QPixmap::fromImage(QImage(document.absoluteFilePath()))));
graphicsViewPreview->fitInView(_previewScene.sceneRect(), Qt::KeepAspectRatio);
qCDebug(EDIM) << _library.data(index, Library::DocumentContentRole).toString();
}
void MainWindow::searchDocument(const QString& text)
{
QList<QFileInfo> matchingFiles(_library.search(text));
qCDebug(EDIM) << matchingFiles.size();
if (matchingFiles.isEmpty()) {
// FIXME filtering currently not yet supported in new library model
// _libraryModel.setNameFilters(DocumentHandler::supportedFileTypes());
} else {
QStringList names;
foreach (const QFileInfo& document, matchingFiles) {
// FIXME introduce proxy model since name filter doesn't apply to pathes
names.append(document.absoluteFilePath());
}
names.append(DocumentHandler::supportedFileTypes());
// _libraryModel.setNameFilters(names);
}
}