18
18
19
19
20
20
#include " FolderAsWorkspaceDock.h"
21
+
22
+ #include < QFileSystemModel>
23
+ #include < QMessageBox>
24
+
21
25
#include " ApplicationSettings.h"
26
+ #include " MainWindow.h"
22
27
#include " ui_FolderAsWorkspaceDock.h"
23
28
24
- # include < QFileSystemModel >
29
+ QString NEW_DIR_TEMPLATE ( " dir_%1 " );
25
30
26
31
ApplicationSetting<QString> rootPathSetting{" FolderAsWorkspace/RootPath" };
27
32
28
- FolderAsWorkspaceDock::FolderAsWorkspaceDock (QWidget *parent) :
33
+ FolderAsWorkspaceDock::FolderAsWorkspaceDock (MainWindow *parent) :
29
34
QDockWidget(parent),
30
- ui(new Ui::FolderAsWorkspaceDock),
31
- model(new QFileSystemModel(this ))
35
+ window(parent)
32
36
{
37
+ ui = new Ui::FolderAsWorkspaceDock;
38
+ model = new QFileSystemModel (this );
33
39
ui->setupUi (this );
34
40
35
41
model->setReadOnly (false );
@@ -72,7 +78,7 @@ void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point)
72
78
{
73
79
QModelIndex index = ui->treeView ->indexAt (point);
74
80
if (!index .isValid ()) {
75
- lastSelectedItem = model->index (0 , 0 );
81
+ lastSelectedItem = model->index (rootPath () );
76
82
ui->menuEmpty ->exec (ui->treeView ->viewport ()->mapToGlobal (point));
77
83
return ;
78
84
}
@@ -85,14 +91,45 @@ void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point)
85
91
}
86
92
}
87
93
88
- void FolderAsWorkspaceDock::on_actionNewFile_triggered ()
94
+ void FolderAsWorkspaceDock::on_actionSaveHere_triggered ()
89
95
{
90
- qInfo (Q_FUNC_INFO);
96
+ QDir parentDir (model->filePath (lastSelectedItem));
97
+ auto doc = window->currentEditor ();
98
+ QString dstName (parentDir.absoluteFilePath (doc->getName ()));
99
+
100
+ if (doc->saveAs (dstName) != QFileDevice::NoError)
101
+ {
102
+ qWarning (" Unable to save %s" , dstName.toUtf8 ().constData ());
103
+ }
104
+ else
105
+ {
106
+ auto newItem = model->index (dstName);
107
+ if (!doc->isFile ()) {
108
+ doc->setFileInfo (dstName);
109
+ }
110
+ ui->treeView ->setCurrentIndex (newItem);
111
+ ui->treeView ->edit (newItem);
112
+ }
91
113
}
92
114
93
115
void FolderAsWorkspaceDock::on_actionNewFolder_triggered ()
94
116
{
95
- qInfo (Q_FUNC_INFO);
117
+ QDir parentDir (model->filePath (lastSelectedItem));
118
+ int i = 1 ;
119
+
120
+ for (;parentDir.exists (NEW_DIR_TEMPLATE.arg (i)); i++) {
121
+ // Intentional
122
+ }
123
+ QString dstName = NEW_DIR_TEMPLATE.arg (i);
124
+
125
+ auto newItem = model->mkdir (lastSelectedItem, dstName);
126
+ if (!newItem.isValid ()) {
127
+ qWarning (" Unable to create %s" , dstName.toUtf8 ().constData ());
128
+ }
129
+ else {
130
+ ui->treeView ->setCurrentIndex (newItem);
131
+ ui->treeView ->edit (newItem);
132
+ }
96
133
}
97
134
98
135
void FolderAsWorkspaceDock::on_actionRename_triggered ()
@@ -103,5 +140,22 @@ void FolderAsWorkspaceDock::on_actionRename_triggered()
103
140
104
141
void FolderAsWorkspaceDock::on_actionDelete_triggered ()
105
142
{
106
- qInfo (Q_FUNC_INFO);
143
+ bool ret;
144
+ QString path (model->filePath (lastSelectedItem));
145
+ QMessageBox::StandardButton reply = QMessageBox::question (this , tr (" Delete Item" ),
146
+ tr (" Are you sure you want to delete <b>%1</b>?" ).arg (path));
147
+
148
+ if (reply == QMessageBox::Yes)
149
+ {
150
+ if (model->isDir (lastSelectedItem)) {
151
+ ret = model->rmdir (lastSelectedItem);
152
+ }
153
+ else {
154
+ ret = model->remove (lastSelectedItem);
155
+ }
156
+ if (!ret)
157
+ {
158
+ qWarning (" Unable to delete %s" , path.toUtf8 ().constData ());
159
+ }
160
+ }
107
161
}
0 commit comments