/
FMPlugin.cpp
323 lines (264 loc) · 8.81 KB
/
FMPlugin.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "FMPlugin.h"
#include <QtCore>
#include <QtGui/QCompleter>
#include <QtGui/QHeaderView>
#include <QtGui/QInputDialog>
#include <QtGui/QLineEdit>
#include <QtGui/QMessageBox>
#include <QtGui/QMenu>
#include <QtGui/QAction>
#include <QtGui/QToolBar>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QCheckBox>
#include <Log.h>
#include <MainSettings.h>
#include <PluginSettings.h>
#include <EditorSettings.h>
#include "ManageDlg.h"
#include "TreeView.h"
FMPlugin::FMPlugin() : QObject(), JuffPlugin() {
showAsTree = PluginSettings::getBool(this, "ShowAsTree", false);
showHidden = PluginSettings::getBool(this, "ShowHidden", false);
sortColumn = PluginSettings::getInt(this, "sortColumn", 0);
w_ = new QWidget();
w_->setWindowTitle(tr("Files"));
QToolBar * toolBar = new QToolBar("File Browser Tools", w_);
//! \todo TODO/FIXME: maybe it could be configured with MainSettings::get(MainSettings::IconSize)...
toolBar->setIconSize(QSize(16, 16));
model_.setRootPath("");
tree_ = new TreeView(this);
tree_->setModel(&model_);
tree_->setDragDropMode(QAbstractItemView::DragOnly);
tree_->setAllColumnsShowFocus(true);
tree_->sortByColumn(sortColumn, Qt::AscendingOrder);
tree_->setSortingEnabled(true);
tree_->setSelectionMode(QAbstractItemView::ExtendedSelection);
tree_->header()->resizeSection(0, 180);
tree_->header()->resizeSection(1, 80);
tree_->header()->resizeSection(2, 60);
tree_->header()->resizeSection(3, 180);
tree_->initMenu();
pathEd_ = new QLineEdit("");
backBtn_ = toolBar->addAction(QIcon(":icon_back"), tr("Go Back"), this, SLOT(back()));
toolBar->addAction(QIcon(":icon_up"), tr("Go Up"), this, SLOT(up()));
toolBar->addAction(QIcon(":icon_home"), tr("Go to Home Directory"), this, SLOT(home()));
toolBar->addAction(QIcon(":icon_current"), tr("Go to current file's directory"), this, SLOT(curFileDir()));
toolBar->addAction(QIcon(":icon_bookmarks"), tr("Favorite Locations"), this, SLOT(favorites()));
toolBar->addAction(QIcon(":icon_new_dir"), tr("New Directory"), this, SLOT(newDir()));
pathEd_->setCompleter(new QCompleter(&model_));
QVBoxLayout* vBox = new QVBoxLayout();
vBox->setMargin(0);
vBox->setSpacing(2);
vBox->addWidget(toolBar);
vBox->addWidget(pathEd_);
vBox->addWidget(tree_);
w_->setLayout(vBox);
connect(tree_, SIGNAL(doubleClicked(const QModelIndex&)), SLOT(itemDoubleClicked(const QModelIndex&)));
connect(tree_, SIGNAL(goUp()), SLOT(up()));
connect(pathEd_, SIGNAL(returnPressed()), SLOT(textEntered()));
connect(&fsWatcher_, SIGNAL(directoryChanged(const QString&)), SLOT(onDirChanged(const QString&)));
QString favStr = PluginSettings::getString(this, "favorites");
if ( !favStr.isEmpty() )
favorites_ = favStr.split(";");
favoritesMenu_ = new QMenu(w_);
addToFavoritesAct_ = new QAction(tr("Add current path"), this);
manageFavoritesAct_ = new QAction(tr("Manage favorites"), this);
connect(addToFavoritesAct_, SIGNAL(triggered()), this, SLOT(addToFavorites()));
connect(manageFavoritesAct_, SIGNAL(triggered()), this, SLOT(manageFavorites()));
initFavoritesMenu();
QString lastDir = PluginSettings::getString(this, "lastDir");
if ( lastDir.isEmpty() )
lastDir = QDir::homePath();
cd(lastDir, false);
backBtn_->setEnabled(false);
applySettings();
}
FMPlugin::~FMPlugin() {
sortColumn = tree_->header()->sortIndicatorSection();
PluginSettings::set(this, "sortColumn", sortColumn);
delete w_;
}
QString FMPlugin::name() const {
return "File manager";
}
QString FMPlugin::targetEngine() const {
return "all";
}
QString FMPlugin::description() const {
return "Simple file manager that lets to open files without a dialog";
}
QWidgetList FMPlugin::dockList() const {
QWidgetList wList;
wList << w_;
return wList;
}
bool FMPlugin::dockVisible(QWidget* w) const {
if ( w == w_ ) {
return true;
}
else {
return JuffPlugin::dockVisible(w);
}
}
void FMPlugin::initFavoritesMenu() {
favoritesMenu_->clear();
favoritesMenu_->addAction(addToFavoritesAct_);
favoritesMenu_->addAction(manageFavoritesAct_);
if ( !favorites_.isEmpty() )
favoritesMenu_->addSeparator();
foreach (QString item, favorites_) {
favoritesMenu_->addAction(item, this, SLOT(goToFavorite()));
}
}
void FMPlugin::cd(const QString& path, bool addToHistory /*= true*/) {
if ( QFileInfo(path).isDir() ) {
// remove currently watching paths
QStringList dirs = fsWatcher_.directories();
if ( !dirs.isEmpty() ) {
fsWatcher_.removePaths(dirs);
}
if ( addToHistory ) {
QString curPath = model_.filePath(tree_->rootIndex());
history_.push(curPath);
if ( !backBtn_->isEnabled() )
backBtn_->setEnabled(true);
}
tree_->setRootIndex(model_.index(path));
pathEd_->setText(path);
pathEd_->setToolTip(path);
PluginSettings::set(this, "lastDir", path);
// add new current path for watching
fsWatcher_.addPath(path);
}
}
void FMPlugin::itemDoubleClicked(const QModelIndex& index) {
QString path = model_.filePath(index);
if ( QFileInfo(path).isDir() ) {
cd(path);
}
else {
api()->openDoc(path);
}
}
void FMPlugin::onDirChanged(const QString&) {
}
void FMPlugin::back() {
if ( !history_.isEmpty() ) {
QString dir = history_.pop();
if ( history_.isEmpty() )
backBtn_->setEnabled(false);
cd(dir, false);
}
}
void FMPlugin::up() {
// keep the current path
QModelIndex curIndex = tree_->rootIndex();
QString prevPath = model_.filePath(curIndex);
QModelIndex parent = curIndex.parent();
if ( parent.isValid() ) {
cd(model_.filePath(parent));
// select the previous dir
QModelIndex prevIndex = model_.index(prevPath);
if ( prevIndex.isValid() ) {
tree_->setCurrentIndex(prevIndex);
}
}
}
void FMPlugin::home() {
cd(QDir::homePath());
}
void FMPlugin::curFileDir() {
Juff::Document* doc = api()->currentDocument();
if ( !doc->isNull() && !doc->isNoname() ) {
cd(QFileInfo(doc->fileName()).absolutePath());
}
}
void FMPlugin::favorites() {
favoritesMenu_->exec(QCursor::pos());
}
void FMPlugin::newDir() {
QString newDirName = QInputDialog::getText(tree_, tr("New directory"), tr("Directory name"));
if (newDirName.isEmpty())
return;
QDir curDir(model_.filePath(tree_->rootIndex()));
if ( !curDir.mkdir(newDirName) ) {
QMessageBox::warning(tree_, tr("Warning"),
tr("Couldn't create a dir named '%1'").arg(newDirName));
}
}
void FMPlugin::goToFavorite() {
QAction* a = qobject_cast<QAction*>(sender());
if ( a ) {
cd(a->text());
}
}
void FMPlugin::addToFavorites() {
QString path = model_.filePath(tree_->rootIndex());
qDebug() << path;
if ( !favorites_.contains(path) ) {
favorites_.append(path);
initFavoritesMenu();
PluginSettings::set(this, "favorites", favorites_.join(";"));
}
}
void FMPlugin::manageFavorites() {
ManageDlg dlg(tree_, this);
dlg.exec();
QString favStr = PluginSettings::getString(this, "favorites");
if ( !favStr.isEmpty() )
favorites_ = favStr.split(";");
else
favorites_ = QStringList();
initFavoritesMenu();
}
void FMPlugin::textEntered() {
if ( QFileInfo(pathEd_->text()).isDir() )
cd(pathEd_->text());
else
pathEd_->setText(model_.filePath(tree_->rootIndex()));
}
void FMPlugin::onDocSaved(const QString& fileName) {
Q_UNUSED(fileName);
}
void FMPlugin::applySettings() {
QPalette plt = tree_->palette();
plt.setColor(QPalette::Base, EditorSettings::get(EditorSettings::DefaultBgColor));
plt.setColor(QPalette::Text, EditorSettings::get(EditorSettings::DefaultFontColor));
// plt.setColor(QPalette::Highlight, EditorSettings::get(EditorSettings::SelectionBgColor));
// plt.setColor(QPalette::HighlightedText, TextDocSettings::selectionTextColor());
tree_->setPalette(plt);
pathEd_->setPalette(plt);
PluginSettings::set(this, "ShowAsTree", showAsTree);
tree_->setRootIsDecorated(showAsTree);
tree_->setItemsExpandable(showAsTree);
QDir::Filters filter = QDir::AllDirs | QDir::AllEntries | QDir::NoDotAndDotDot;
if (showHidden)
filter |= QDir::Hidden;
model_.setFilter(filter);
}
QWidget * FMPlugin::settingsPage() const
{
QWidget * cfg = new QWidget();
QVBoxLayout * l = new QVBoxLayout();
cfg->setLayout(l);
QCheckBox * treeCheckBox = new QCheckBox(tr("Show Directory Structure in a Tree"), cfg);
treeCheckBox->setChecked(showAsTree);
connect(treeCheckBox, SIGNAL(toggled(bool)), this, SLOT(treeCheckBox_toggled(bool)));
QCheckBox *showHiddenBox = new QCheckBox(tr("Show Hidden Files and Directories"), cfg);
showHiddenBox->setChecked(showHidden);
connect(showHiddenBox, SIGNAL(toggled(bool)), this, SLOT(showHiddenBox_toggled(bool)));
l->addWidget(treeCheckBox);
l->addWidget(showHiddenBox);
l->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
return cfg;
}
void FMPlugin::treeCheckBox_toggled(bool value)
{
showAsTree = value;
}
void FMPlugin::showHiddenBox_toggled(bool value)
{
showHidden = value;
}
Q_EXPORT_PLUGIN2(fm, FMPlugin)