forked from Tulon/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelinkingDialog.cpp
156 lines (130 loc) · 4.9 KB
/
RelinkingDialog.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) Joseph Artsimovich <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RelinkingDialog.h"
#include "RelinkingDialog.h.moc"
#include "RelinkingSortingModel.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include <boost/foreach.hpp>
#include <assert.h>
RelinkingDialog::RelinkingDialog(QString const& project_file_path, QWidget* parent)
: QDialog(parent)
, m_pSortingModel(new RelinkingSortingModel)
, m_projectFileDir(QFileInfo(project_file_path).path())
{
ui.setupUi(this);
m_pSortingModel->setSourceModel(&m_model);
ui.listView->setModel(m_pSortingModel);
ui.listView->setTextElideMode(Qt::ElideMiddle);
ui.errorLabel->setVisible(false);
ui.undoButton->setVisible(false);
connect(
ui.listView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection const&, QItemSelection const&)),
SLOT(selectionChanged(QItemSelection const&, QItemSelection const&))
);
connect(
ui.pathVisualization, SIGNAL(clicked(QString const&, QString const&, int)),
SLOT(pathButtonClicked(QString const&, QString const&, int))
);
connect(ui.undoButton, SIGNAL(clicked()), SLOT(undoButtonClicked()));
disconnect(ui.buttonBox, SIGNAL(accepted()));
connect(ui.buttonBox, SIGNAL(accepted()), SLOT(commitChanges()));
}
void
RelinkingDialog::selectionChanged(QItemSelection const& selected, QItemSelection const& deselected)
{
if (selected.isEmpty()) {
ui.pathVisualization->clear();
ui.pathVisualization->setVisible(false);
} else {
ui.undoButton->setVisible(false);
QModelIndex const index(selected.front().topLeft());
QString const path(index.data(m_model.UncommittedPathRole).toString());
int const type = index.data(m_model.TypeRole).toInt();
ui.pathVisualization->setPath(RelinkablePath(path, (RelinkablePath::Type)type), /*clickable=*/true);
ui.pathVisualization->setVisible(true);
if (ui.errorLabel->isVisible()) {
m_model.rollbackChanges();
} else {
m_model.commitChanges();
}
}
ui.errorLabel->setVisible(false);
}
void
RelinkingDialog::pathButtonClicked(
QString const& prefix_path, QString const& suffix_path, int const type)
{
assert(!prefix_path.endsWith(QChar('/')) && !prefix_path.endsWith(QChar('\\')));
assert(!suffix_path.startsWith(QChar('/')) && !suffix_path.startsWith(QChar('\\')));
QString replacement_path;
if (type == RelinkablePath::File) {
QDir const dir(QFileInfo(prefix_path).dir());
replacement_path = QFileDialog::getOpenFileName(
this, tr("Substitution File for %1").arg(QDir::toNativeSeparators(prefix_path)),
dir.exists() ? dir.path() : m_projectFileDir,
QString(), 0, QFileDialog::DontUseNativeDialog
);
} else {
QDir const dir(prefix_path);
replacement_path = QFileDialog::getExistingDirectory(
this, tr("Substitution Directory for %1").arg(QDir::toNativeSeparators(prefix_path)),
dir.exists() ? prefix_path : m_projectFileDir,
QFileDialog::DontUseNativeDialog
);
}
// So what's wrong with native dialogs? The one for directory selection won't show files
// at all (if you ask it to, the non-native dialog will appear), which is inconvenient
// in this situation. So, if one of them has to be non-native, the other was made
// non-native as well, for consistency reasons.
replacement_path = RelinkablePath::normalize(replacement_path);
if (replacement_path.isEmpty()) {
return;
}
if (prefix_path == replacement_path) {
return;
}
QString new_path(replacement_path);
new_path += QChar('/');
new_path += suffix_path;
m_model.replacePrefix(prefix_path, replacement_path, (RelinkablePath::Type)type);
if (m_model.checkForMerges()) {
ui.errorLabel->setText(
tr("This change would merge several files into one.")
);
ui.errorLabel->setVisible(true);
ui.pathVisualization->clear();
ui.pathVisualization->setVisible(false);
} else {
ui.pathVisualization->setPath(RelinkablePath(new_path, (RelinkablePath::Type)type), /*clickable=*/false);
ui.pathVisualization->setVisible(true);
}
ui.undoButton->setVisible(true);
ui.listView->update();
}
void
RelinkingDialog::undoButtonClicked()
{
m_model.rollbackChanges(); // Has to go before selectionChanged()
selectionChanged(ui.listView->selectionModel()->selection(), QItemSelection());
}
void
RelinkingDialog::commitChanges()
{
m_model.commitChanges();
accept();
}