ui: Refactored context menu API, added file path actions in graph and code

* Add 'copy full path' and 'show containing folder' actions to graph view, code view
* Added file path to file node tooltip

bug id = 320
This commit is contained in:
Eberhard Graether
2017-01-26 21:54:57 +01:00
parent 4fc0785002
commit 2aa8c4c22c
11 changed files with 185 additions and 94 deletions
+78 -31
View File
@@ -1,59 +1,83 @@
#include "qt/utility/QtContextMenu.h"
#include <QMenu>
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QDesktopServices>
#include <QUrl>
#include "utility/messaging/type/MessageRedo.h"
#include "utility/messaging/type/MessageUndo.h"
std::shared_ptr<QtContextMenu> QtContextMenu::s_instance;
QtContextMenu* QtContextMenu::s_instance;
QtContextMenu* QtContextMenu::getInstance()
QAction* QtContextMenu::s_undoAction;
QAction* QtContextMenu::s_redoAction;
QAction* QtContextMenu::s_copyFullPathAction;
QAction* QtContextMenu::s_openContainingFolderAction;
FilePath QtContextMenu::s_filePath;
QtContextMenu::QtContextMenu(QContextMenuEvent* event, QWidget* origin)
: m_menu(origin)
, m_point(event->globalPos())
{
if (!s_instance)
{
s_instance = std::make_shared<QtContextMenu>();
s_instance = new QtContextMenu();
s_undoAction = new QAction(tr("Back"), s_instance);
s_undoAction->setStatusTip(tr("Go back to last active symbol"));
s_undoAction->setToolTip(tr("Go back to last active symbol"));
connect(s_undoAction, SIGNAL(triggered()), s_instance, SLOT(undoActionTriggered()));
s_redoAction = new QAction(tr("Forward"), s_instance);
s_redoAction->setStatusTip(tr("Go forward to next active symbol"));
s_redoAction->setToolTip(tr("Go forward to next active symbol"));
connect(s_redoAction, SIGNAL(triggered()), s_instance, SLOT(redoActionTriggered()));
s_copyFullPathAction = new QAction(tr("Copy Full Path"), s_instance);
s_copyFullPathAction->setStatusTip(tr("Copies the path of this file to the clipboard"));
s_copyFullPathAction->setToolTip(tr("Copies the path of this file to the clipboard"));
connect(s_copyFullPathAction, SIGNAL(triggered()), s_instance, SLOT(copyFullPathActionTriggered()));
s_openContainingFolderAction = new QAction(tr("Open Containing Folder"), s_instance);
s_openContainingFolderAction->setStatusTip(tr("Opens the folder that contains this file"));
s_openContainingFolderAction->setToolTip(tr("Opens the folder that contains this file"));
connect(s_openContainingFolderAction, SIGNAL(triggered()), s_instance, SLOT(openContainingFolderActionTriggered()));
}
return s_instance.get();
addUndoActions();
}
QtContextMenu::QtContextMenu()
void QtContextMenu::addAction(QAction* action)
{
m_undoAction = new QAction(tr("Back"), this);
m_undoAction->setStatusTip(tr("Go back to last active symbol"));
m_undoAction->setToolTip(tr("Go back to last active symbol"));
connect(m_undoAction, SIGNAL(triggered()), this, SLOT(undoActionTriggered()));
m_redoAction = new QAction(tr("Forward"), this);
m_redoAction->setStatusTip(tr("Go forward to next active symbol"));
m_redoAction->setToolTip(tr("Go forward to next active symbol"));
connect(m_redoAction, SIGNAL(triggered()), this, SLOT(redoActionTriggered()));
m_menu.addAction(action);
}
void QtContextMenu::showDefault(QContextMenuEvent* event, QWidget* origin)
void QtContextMenu::addUndoActions()
{
showExtended(event, origin, std::vector<QAction*>());
addAction(s_undoAction);
addAction(s_redoAction);
}
void QtContextMenu::showExtended(QContextMenuEvent* event, QWidget* origin, const std::vector<QAction*>& actions)
void QtContextMenu::addFileActions(FilePath filePath)
{
QMenu menu(origin);
s_filePath = filePath;
menu.addAction(m_undoAction);
menu.addAction(m_redoAction);
addAction(s_copyFullPathAction);
addAction(s_openContainingFolderAction);
}
if (actions.size())
{
menu.addSeparator();
void QtContextMenu::addSeparator()
{
m_menu.addSeparator();
}
for (QAction* action : actions)
{
menu.addAction(action);
}
}
menu.exec(event->globalPos());
void QtContextMenu::show()
{
m_menu.exec(m_point);
}
void QtContextMenu::undoActionTriggered()
@@ -65,3 +89,26 @@ void QtContextMenu::redoActionTriggered()
{
MessageRedo().dispatch();
}
void QtContextMenu::copyFullPathActionTriggered()
{
const std::string pathString = (QSysInfo::windowsVersion() != QSysInfo::WV_None) ? s_filePath.getBackslashedString() : s_filePath.str();
QApplication::clipboard()->setText(pathString.c_str());
}
void QtContextMenu::openContainingFolderActionTriggered()
{
FilePath dir = s_filePath.parentDirectory();
if (dir.exists())
{
QDesktopServices::openUrl(QUrl(("file:///" + dir.str()).c_str(), QUrl::TolerantMode));
}
else
{
LOG_ERROR("Unable to open directory: " + dir.str());
}
}
QtContextMenu::QtContextMenu()
{
}
+28 -8
View File
@@ -3,30 +3,50 @@
#include <memory>
#include <QAction>
#include <QMenu>
#include <QObject>
#include "utility/file/FilePath.h"
#include "utility/logging/logging.h"
class QtContextMenu
: public QObject
{
Q_OBJECT
public:
static QtContextMenu* getInstance();
QtContextMenu(QContextMenuEvent* event, QWidget* origin);
QtContextMenu();
void addAction(QAction* action);
void addUndoActions();
void addFileActions(FilePath filePath);
void showDefault(QContextMenuEvent* event, QWidget* origin);
void showExtended(QContextMenuEvent* event, QWidget* origin, const std::vector<QAction*>& actions);
void addSeparator();
void show();
private slots:
void undoActionTriggered();
void redoActionTriggered();
private:
static std::shared_ptr<QtContextMenu> s_instance;
void copyFullPathActionTriggered();
void openContainingFolderActionTriggered();
QAction* m_undoAction;
QAction* m_redoAction;
private:
QtContextMenu();
static QtContextMenu* s_instance;
static QAction* s_undoAction;
static QAction* s_redoAction;
static QAction* s_copyFullPathAction;
static QAction* s_openContainingFolderAction;
static FilePath s_filePath;
QMenu m_menu;
QPoint m_point;
};
#endif // QT_CONTEXT_MENU_H