Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context Menu operations on Folder as Workspace tree #556

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Next Next commit
Workspace operations: Rename
  • Loading branch information
SimLV committed May 7, 2024
commit 882066cd98562f29f97da096b1a21f0eb32f9611
40 changes: 40 additions & 0 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(QWidget *parent) :
{
ui->setupUi(this);

model->setReadOnly(false);
ui->treeView->setModel(model);
ui->treeView->header()->hideSection(1);
ui->treeView->header()->hideSection(2);
Expand All @@ -42,6 +43,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(QWidget *parent) :
emit fileDoubleClicked(model->filePath(index));
}
});
connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &FolderAsWorkspaceDock::onCustomContextMenu);

ApplicationSettings settings;
setRootPath(settings.get(rootPathSetting));
Expand All @@ -65,3 +67,41 @@ QString FolderAsWorkspaceDock::rootPath() const
{
return model->rootPath();
}

void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point)
{
QModelIndex index = ui->treeView->indexAt(point);
if (!index.isValid()) {
lastSelectedItem = model->index(0, 0);
ui->menuEmpty->exec(ui->treeView->viewport()->mapToGlobal(point));
return;
}
lastSelectedItem = index;
if (model->isDir(index)) {
ui->menuDirectory->exec(ui->treeView->viewport()->mapToGlobal(point));
}
else {
ui->menuFile->exec(ui->treeView->viewport()->mapToGlobal(point));
}
}

void FolderAsWorkspaceDock::on_actionNewFile_triggered()
{
qInfo(Q_FUNC_INFO);
}

void FolderAsWorkspaceDock::on_actionNewFolder_triggered()
{
qInfo(Q_FUNC_INFO);
}

void FolderAsWorkspaceDock::on_actionRename_triggered()
{
ui->treeView->setCurrentIndex(lastSelectedItem);
ui->treeView->edit(lastSelectedItem);
}

void FolderAsWorkspaceDock::on_actionDelete_triggered()
{
qInfo(Q_FUNC_INFO);
}
10 changes: 10 additions & 0 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define FOLDERASWORKSPACEDOCK_H

#include <QDockWidget>
#include <QModelIndex>

namespace Ui {
class FolderAsWorkspaceDock;
Expand All @@ -42,10 +43,19 @@ class FolderAsWorkspaceDock : public QDockWidget
signals:
void fileDoubleClicked(const QString &filePath);

private slots:
void on_actionNewFile_triggered();
void on_actionNewFolder_triggered();
void on_actionRename_triggered();
void on_actionDelete_triggered();

void onCustomContextMenu(const QPoint &point);

private:
Ui::FolderAsWorkspaceDock *ui;

QFileSystemModel *model;
QModelIndex lastSelectedItem;
};

#endif // FOLDERASWORKSPACEDOCK_H
64 changes: 63 additions & 1 deletion src/NotepadNext/docks/FolderAsWorkspaceDock.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,81 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QMenu" name="menuFile">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can QtCreator create a QMenu in the ui file? or some other method (e.g. manually)? I'm just interested since I've never seen this done before :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't. I googled for it but at least Qt 5 can't. For me it looks like minor inconvenience everyone accustomed to.

<property name="title">
<string>File Menu</string>
</property>
<addaction name="actionRename"/>
<addaction name="actionDelete"/>
</widget>
</item>
<item>
<widget class="QMenu" name="menuDirectory">
<property name="title">
<string>Folder Menu</string>
</property>
<addaction name="actionRename"/>
<addaction name="actionDelete"/>
<addaction name="separator"/>
<addaction name="actionNewFolder"/>
<addaction name="actionNewFile"/>
</widget>
</item>
<item>
<widget class="QMenu" name="menuEmpty">
<property name="title">
<string>Space Menu</string>
</property>
<addaction name="actionNewFolder"/>
<addaction name="actionNewFile"/>
</widget>
</item>
<item>
<widget class="QTreeView" name="treeView">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed</set>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<action name="actionNewFile">
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/newfile.png</normaloff>:/icons/newfile.png</iconset>
</property>
<property name="text">
<string>&amp;New File</string>
</property>
</action>
<action name="actionNewFolder">
<property name="text">
<string>New &amp;Folder</string>
</property>
</action>
<action name="actionRename">
<property name="text">
<string>&amp;Rename</string>
</property>
</action>
<action name="actionDelete">
<property name="text">
<string>&amp;Delete</string>
</property>
</action>
</widget>
<resources/>
<resources>
<include location="../resources.qrc"/>
</resources>
<connections/>
</ui>