26 #include <QContextMenuEvent>
27 #include <QHeaderView>
28 #include <QInputDialog>
29 #include <QItemSelectionModel>
31 #include <QMessageBox>
39 WorkspaceView::WorkspaceView(
QWidget* parent)
42 project_widget(nullptr)
44 action_open =
new QAction(QIcon::fromTheme(
"document-open"),
"Open",
this);
45 action_rename =
new QAction(
"Rename...",
this);
46 action_rename->setShortcut(QKeySequence(
"F2"));
47 action_rename->setShortcutContext(Qt::WidgetShortcut);
48 action_remove =
new QAction(QIcon::fromTheme(
"edit-delete"),
"Remove",
this);
49 action_remove->setShortcut(QKeySequence(
"Del"));
50 action_remove->setShortcutContext(Qt::WidgetShortcut);
51 action_close =
new QAction(
"Close",
this);
52 action_close->setShortcutContext(Qt::WidgetShortcut);
55 addAction(action_open);
56 addAction(action_rename);
57 addAction(action_remove);
58 addAction(action_close);
61 action_open->setEnabled(
false);
62 action_rename->setEnabled(
false);
63 action_remove->setEnabled(
false);
64 action_close->setEnabled(
false);
66 connect(action_open, SIGNAL(triggered()),
this, SLOT(SLOT_Open()));
67 connect(action_rename, SIGNAL(triggered()),
this, SLOT(SLOT_RenameDialog()));
68 connect(action_remove, SIGNAL(triggered()),
this, SLOT(SLOT_Remove()));
69 connect(action_close, SIGNAL(triggered()),
this, SLOT(SLOT_Close()));
78 action_open->setEnabled(
false);
79 action_rename->setEnabled(
false);
80 action_remove->setEnabled(
false);
81 action_close->setEnabled(
false);
85 QTreeView::setModel(model);
88 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
const &, QItemSelection
const &)),
89 this, SLOT(SLOT_SelectionChanged(QItemSelection
const &, QItemSelection
const &)));
100 return action_rename;
105 return action_remove;
108 void WorkspaceView::SLOT_Close()
110 QModelIndexList list = selectedIndexes();
114 QModelIndex
const& index = list.first();
116 action_rename->setEnabled(
true);
119 void WorkspaceView::SLOT_Open()
121 QModelIndexList list = selectedIndexes();
125 QModelIndex
const& index = list.first();
129 action_rename->setEnabled(
false);
130 }
catch (exception& e) {
131 QMessageBox::warning(
this,
"Could not open file", e.what());
135 void WorkspaceView::SLOT_RenameDialog()
137 QModelIndexList list = selectedIndexes();
141 QModelIndex index = list.first();
142 if (model->GetType(index) == WorkspaceQtModel::FileType) {
143 index = model->parent(index);
145 string const old_name = model->GetName(index);
147 string const new_name = QInputDialog::getText(
this,
"Rename project",
"Please enter a new name:",
148 QLineEdit::Normal, QString::fromStdString(old_name), &ok).toStdString();
149 if (ok && (new_name != old_name)) {
151 model->Workspace()->Rename(old_name, new_name);
153 if (project_widget_project_name == old_name) {
154 project_widget.reset();
155 project_widget_project_name.clear();
157 emit ProjectRenamed(old_name, new_name);
159 QMessageBox::warning(
this,
"Error", QString(
"Could not rename project: ") + e.
what());
164 void WorkspaceView::SLOT_Remove()
166 QModelIndexList list = selectedIndexes();
170 QModelIndex index = list.first();
172 if (model->GetType(index) == WorkspaceQtModel::FileType) {
173 if (model->rowCount(model->parent(index)) == 1) {
174 if (QMessageBox::warning(
this,
"Continue?",
175 "This is the only file in this project.\nRemoving it will remove the project.\nContinue?",
176 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
177 index = model->parent(index);
178 string project_name = model->GetName(index);
181 model->Workspace()->Remove(project_name);
183 if (project_widget_project_name == project_name) {
184 project_widget.reset();
185 project_widget_project_name.clear();
187 emit ProjectRemoved(project_name);
189 QMessageBox::warning(
this,
"Error", QString(
"Could not remove project: ") + e.
what());
193 if (QMessageBox::warning(
this,
"Warning",
"Remove file?\nThis cannot be undone.",
194 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
195 string project_name = model->GetName(model->parent(index));
196 string file_name = model->GetName(index);
198 auto project = model->Workspace()->Get(project_name);
199 project->Remove(file_name);
201 QMessageBox::warning(
this,
"Error", QString(
"Could not remove file:") + e.
what());
206 }
else if (model->GetType(index) == WorkspaceQtModel::ProjectType) {
207 if (QMessageBox::warning(
this,
"Warning",
"Remove project?\nThis cannot be undone.",
208 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
209 string project_name = model->GetName(index);
212 model->Workspace()->Remove(project_name);
214 if (project_widget_project_name == project_name) {
215 project_widget.reset();
216 project_widget_project_name.clear();
218 emit ProjectRemoved(project_name);
221 QMessageBox::warning(
this,
"Error", QString(
"Could not remove project.") + e.
what());
227 void WorkspaceView::SLOT_SelectionChanged(QItemSelection
const &, QItemSelection
const &)
229 if (QItemSelectionModel* s_model = selectionModel()) {
230 QModelIndexList indexes = s_model->selection().indexes();
231 if (indexes.empty()) {
233 action_open->setEnabled(
false);
234 action_rename->setEnabled(
false);
235 action_remove->setEnabled(
false);
236 action_close->setEnabled(
false);
238 action_remove->setEnabled(
true);
240 QModelIndex index = indexes.first();
242 action_close->setEnabled(model->IsOpened(index));
244 if (model->GetType(index) == WorkspaceQtModel::ProjectType) {
245 action_open->setEnabled(
true);
246 action_rename->setEnabled(!model->IsOpened(index));
247 }
else if (model->GetType(index) == WorkspaceQtModel::FileType) {
248 action_open->setEnabled(
true);
249 action_rename->setEnabled(
false);
254 action_open->setEnabled(
false);
255 action_rename->setEnabled(
false);
256 action_remove->setEnabled(
false);
257 action_close->setEnabled(
false);
261 void WorkspaceView::SLOT_ProjectWidgetAction()
263 auto& entry = object_to_callback_map[sender()];
264 auto prefs = Ws::MergedPreferences::Create(
266 model->Workspace()->Get(entry.project_name));
267 project_widget = entry.callback(prefs,
this);
269 project_widget_project_name = entry.project_name;
276 QModelIndexList list = selectedIndexes();
278 QModelIndex
const & index = list.first();
279 menu.addAction(action_open);
281 if (model->GetType(index) == WorkspaceQtModel::ProjectType) {
282 object_to_callback_map.clear();
283 auto project = model->Workspace()->Get(model->GetName(index));
284 bool project_is_opened = model->IsOpened(index);
285 for (
auto widget : project->GetWidgets()) {
286 auto a = menu.addAction(QString::fromStdString(widget.name));
287 if (project_is_opened)
288 a->setEnabled(
false);
289 object_to_callback_map[a] = {widget.callback, model->GetName(index)};
292 connect(a, SIGNAL(triggered()),
this, SLOT(SLOT_ProjectWidgetAction()));
295 for (
auto& action : model->GetContextMenuActions(index)) {
296 menu.addAction(action);
298 if (model->GetType(index) == WorkspaceQtModel::ProjectType) {
299 menu.addAction(action_rename);
301 menu.addAction(action_remove);
303 action_close->setEnabled(model->IsOpened(index));
304 menu.addAction(action_close);
306 menu.exec(event->globalPos());
311 auto const& list = selectedIndexes();
313 auto const& index = list.first();
314 WorkspaceQtModel::ItemType type = model->GetType(index);
315 if (type == WorkspaceQtModel::FileType) {
316 return (
void) SLOT_Open();
321 return (
void) QTreeView::mouseDoubleClickEvent(e);
virtual void mouseDoubleClickEvent(QMouseEvent *)
Reimplemented from QTreeView.
QAction * GetOpenAction() const
Get 'open' action object.
see the online Qt documentation
Namespace for miscellaneous utilities.
QAction * GetRemoveAction() const
Get 'remove' action object.
virtual const char * what() const noexcept
Return error message.
Interface for WorkspaceQtModel.
Extremely simple Exception root class.
see the online Qt documentation
Interface for MergedPreferences.
virtual void contextMenuEvent(QContextMenuEvent *)
Reimplemented from QWidget.
QAction * GetRenameAction() const
Get 'rename' action object.
Interface for WorkspaceView.
see the online Qt documentation
Header file for Exception class.
virtual void setModel(QAbstractItemModel *)
Set the model.
Namespace for generic graphical shell for simulators.
Abstraction of workspace on the filesystem.