/
fileorganiserwindowwindow.cpp
221 lines (154 loc) · 7.74 KB
/
fileorganiserwindowwindow.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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.
OpenCOR 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 <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// File Organiser window
//==============================================================================
#include "coreguiutils.h"
#include "fileorganiserwindowwidget.h"
#include "fileorganiserwindowwindow.h"
#include "toolbarwidget.h"
//==============================================================================
#include "ui_fileorganiserwindowwindow.h"
//==============================================================================
#include <QFileIconProvider>
#include <QMenu>
#include <QPoint>
#include <QSettings>
//==============================================================================
namespace OpenCOR {
namespace FileOrganiserWindow {
//==============================================================================
FileOrganiserWindowWindow::FileOrganiserWindowWindow(QWidget *pParent) :
Core::OrganisationWidget(pParent),
mGui(new Ui::FileOrganiserWindowWindow)
{
// Set up the GUI
mGui->setupUi(this);
connect(mGui->actionNew, &QAction::triggered,
this, &FileOrganiserWindowWindow::actionNewTriggered);
connect(mGui->actionDelete, &QAction::triggered,
this, &FileOrganiserWindowWindow::actionDeleteTriggered);
// Create a tool bar widget with different buttons
// Note: normally, we would retrieve the folder icon through a call to
// QFileIconProvider().icon(QFileIconProvider::Folder), but on Windows
// it will, in this case, return the QStyle::SP_DirIcon icon while we
// really want the QStyle::SP_DirClosedIcon icon...
static const QIcon PlusIcon = QIcon(":/oxygen/actions/list-add.png");
auto toolBarWidget = new Core::ToolBarWidget();
QIcon folderIcon = Core::standardIcon(QStyle::SP_DirClosedIcon);
int folderIconSize = folderIcon.availableSizes().first().width();
int plusIconSize = int(0.57*folderIconSize);
int scaledIconSize = devicePixelRatio()*toolBarWidget->iconSize().width();
// Note: we scale the icon in case we are on a non-HiDPI screen, in which
// case the icon would be smaller than what we need for our tool bar
// widget...
mGui->actionNew->setIcon(Core::scaledIcon(Core::overlayedIcon(folderIcon, PlusIcon,
folderIconSize, folderIconSize,
folderIconSize-plusIconSize, 0,
plusIconSize, plusIconSize),
scaledIconSize, scaledIconSize,
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
toolBarWidget->addAction(mGui->actionNew);
toolBarWidget->addAction(mGui->actionDelete);
mGui->layout->addWidget(toolBarWidget);
// Create and add the file organiser widget
mFileOrganiserWindowWidget = new FileOrganiserWindowWidget(this);
mFileOrganiserWindowWidget->setObjectName("FileOrganiserWindowWidget");
mGui->layout->addWidget(mFileOrganiserWindowWidget);
// Create and populate our context menu
mContextMenu = new QMenu(this);
mContextMenu->addAction(mGui->actionNew);
mContextMenu->addAction(mGui->actionDelete);
// We want our own context menu for the file organiser widget
mFileOrganiserWindowWidget->setContextMenuPolicy(Qt::CustomContextMenu);
// Some connections
connect(mFileOrganiserWindowWidget, &FileOrganiserWindowWidget::customContextMenuRequested,
this, &FileOrganiserWindowWindow::showCustomContextMenu);
connect(mFileOrganiserWindowWidget, &FileOrganiserWindowWidget::doubleClicked,
this, &FileOrganiserWindowWindow::itemDoubleClicked);
connect(mFileOrganiserWindowWidget, &FileOrganiserWindowWidget::openFilesRequested,
this, &FileOrganiserWindowWindow::openFiles);
// Some connections to update the enabled state of our various actions
connect(mFileOrganiserWindowWidget, &FileOrganiserWindowWidget::newFolderEnabled,
mGui->actionNew, &QAction::setEnabled);
connect(mFileOrganiserWindowWidget, &FileOrganiserWindowWidget::deleteItemsEnabled,
mGui->actionDelete, &QAction::setEnabled);
}
//==============================================================================
FileOrganiserWindowWindow::~FileOrganiserWindowWindow()
{
// Delete the GUI
delete mGui;
}
//==============================================================================
void FileOrganiserWindowWindow::retranslateUi()
{
// Retranslate our whole window
mGui->retranslateUi(this);
// Retranslate our file organiser widget
mFileOrganiserWindowWidget->retranslateUi();
}
//==============================================================================
void FileOrganiserWindowWindow::loadSettings(QSettings &pSettings)
{
// Retrieve the settings of the file organiser widget
pSettings.beginGroup(mFileOrganiserWindowWidget->objectName());
mFileOrganiserWindowWidget->loadSettings(pSettings);
pSettings.endGroup();
}
//==============================================================================
void FileOrganiserWindowWindow::saveSettings(QSettings &pSettings) const
{
// Keep track of the settings of the file organiser widget
pSettings.beginGroup(mFileOrganiserWindowWidget->objectName());
mFileOrganiserWindowWidget->saveSettings(pSettings);
pSettings.endGroup();
}
//==============================================================================
void FileOrganiserWindowWindow::actionNewTriggered()
{
// Create a new folder
mFileOrganiserWindowWidget->newFolder();
}
//==============================================================================
void FileOrganiserWindowWindow::actionDeleteTriggered()
{
// Remove the current item(s)
mFileOrganiserWindowWidget->deleteItems();
}
//==============================================================================
void FileOrganiserWindowWindow::showCustomContextMenu() const
{
// Show our context menu which items match the contents of our tool bar
// widget
mContextMenu->exec(QCursor::pos());
}
//==============================================================================
void FileOrganiserWindowWindow::itemDoubleClicked(const QModelIndex &pItemIndex)
{
// Check what kind of item has been double clicked and if it is a file, then
// open it
QString fileName = mFileOrganiserWindowWidget->filePath(pItemIndex);
if (!fileName.isEmpty()) {
// We are dealing with a file (as opposed to a folder), so just open it
openFile(fileName);
}
}
//==============================================================================
} // namespace FileOrganiserWindow
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================