NOISSUE add context menus to pages with toolbars

This commit is contained in:
Petr Mrázek 2019-07-25 01:02:30 +02:00
parent c3e61536a3
commit 7dfe73df0c
14 changed files with 151 additions and 16 deletions

View File

@ -43,20 +43,23 @@ AccountListPage::AccountListPage(QWidget *parent)
"If you're new here, you can click the \"Add\" button to add your Mojang or Minecraft account."
));
ui->listView->setEmptyMode(VersionListView::String);
ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
m_accounts = MMC->accounts();
ui->listView->setModel(m_accounts.get());
ui->listView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
// Expand the account column
ui->listView->header()->setSectionResizeMode(1, QHeaderView::Stretch);
QItemSelectionModel *selectionModel = ui->listView->selectionModel();
connect(selectionModel, &QItemSelectionModel::selectionChanged,
[this](const QItemSelection &sel, const QItemSelection &dsel)
{ updateButtonStates(); });
connect(selectionModel, &QItemSelectionModel::selectionChanged, [this](const QItemSelection &sel, const QItemSelection &dsel) {
updateButtonStates();
});
connect(ui->listView, &VersionListView::customContextMenuRequested, this, &AccountListPage::ShowContextMenu);
connect(m_accounts.get(), SIGNAL(listChanged()), SLOT(listChanged()));
connect(m_accounts.get(), SIGNAL(activeAccountChanged()), SLOT(listChanged()));
@ -69,6 +72,13 @@ AccountListPage::~AccountListPage()
delete ui;
}
void AccountListPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->listView->mapToGlobal(pos));
delete menu;
}
void AccountListPage::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)

View File

@ -85,6 +85,7 @@ protected:
protected
slots:
void ShowContextMenu(const QPoint &pos);
void addAccount(const QString& errMsg="");
private:

View File

@ -32,10 +32,17 @@
#include "minecraft/ComponentList.h"
#include <DesktopServices.h>
ModFolderPage::ModFolderPage(BaseInstance *inst, std::shared_ptr<SimpleModList> mods, QString id,
QString iconName, QString displayName, QString helpPage,
QWidget *parent)
: QMainWindow(parent), ui(new Ui::ModFolderPage)
ModFolderPage::ModFolderPage(
BaseInstance *inst,
std::shared_ptr<SimpleModList> mods,
QString id,
QString iconName,
QString displayName,
QString helpPage,
QWidget *parent
) :
QMainWindow(parent),
ui(new Ui::ModFolderPage)
{
ui->setupUi(this);
ui->actionsToolbar->insertSpacer(ui->actionView_configs);
@ -57,6 +64,9 @@ ModFolderPage::ModFolderPage(BaseInstance *inst, std::shared_ptr<SimpleModList>
ui->modTreeView->setModel(m_filterModel);
ui->modTreeView->installEventFilter(this);
ui->modTreeView->sortByColumn(1, Qt::AscendingOrder);
ui->modTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->modTreeView, &ModListView::customContextMenuRequested, this, &ModFolderPage::ShowContextMenu);
auto smodel = ui->modTreeView->selectionModel();
connect(smodel, &QItemSelectionModel::currentChanged, this, &ModFolderPage::modCurrent);
connect(ui->filterEdit, &QLineEdit::textChanged, this, &ModFolderPage::on_filterTextChanged );
@ -70,6 +80,13 @@ QMenu * ModFolderPage::createPopupMenu()
return filteredMenu;
}
void ModFolderPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->actionsToolbar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->modTreeView->mapToGlobal(pos));
delete menu;
}
void ModFolderPage::openedImpl()
{
m_mods->startWatching();

View File

@ -102,6 +102,7 @@ slots:
void on_actionDisable_triggered();
void on_actionView_Folder_triggered();
void on_actionView_configs_triggered();
void ShowContextMenu(const QPoint &pos);
};
class CoreModFolderPage : public ModFolderPage

View File

@ -235,6 +235,8 @@ ScreenshotsPage::ScreenshotsPage(QString path, QWidget *parent)
ui->listView->installEventFilter(this);
ui->listView->setEditTriggers(0);
ui->listView->setItemDelegate(new CenteredEditingDelegate(this));
ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->listView, &QListView::customContextMenuRequested, this, &ScreenshotsPage::ShowContextMenu);
connect(ui->listView, SIGNAL(activated(QModelIndex)), SLOT(onItemActivated(QModelIndex)));
}
@ -266,6 +268,13 @@ ScreenshotsPage::~ScreenshotsPage()
delete ui;
}
void ScreenshotsPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->listView->mapToGlobal(pos));
delete menu;
}
QMenu * ScreenshotsPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();

View File

@ -77,6 +77,7 @@ private slots:
void on_actionRename_triggered();
void on_actionView_Folder_triggered();
void onItemActivated(QModelIndex);
void ShowContextMenu(const QPoint &pos);
private:
Ui::ScreenshotsPage *ui;

View File

@ -564,6 +564,9 @@ ServersPage::ServersPage(MinecraftInstance * inst, QWidget* parent)
m_model = new ServersModel(inst->gameRoot(), this);
ui->serversView->setIconSize(QSize(64,64));
ui->serversView->setModel(m_model);
ui->serversView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->serversView, &QTreeView::customContextMenuRequested, this, &ServersPage::ShowContextMenu);
auto head = ui->serversView->header();
if(head->count())
{
@ -596,6 +599,13 @@ ServersPage::~ServersPage()
m_model->saveNow();
}
void ServersPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->serversView->mapToGlobal(pos));
delete menu;
}
QMenu * ServersPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();

View File

@ -79,7 +79,9 @@ private slots:
void nameEdited(const QString & name);
void addressEdited(const QString & address);
void resourceIndexChanged(int index);
void resourceIndexChanged(int index);\
void ShowContextMenu(const QPoint &pos);
private: // data
int currentServer = -1;

View File

@ -123,6 +123,8 @@ VersionPage::VersionPage(MinecraftInstance *inst, QWidget *parent)
ui->packageView->setModel(proxy);
ui->packageView->installEventFilter(this);
ui->packageView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->packageView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->packageView->selectionModel(), &QItemSelectionModel::currentChanged, this, &VersionPage::versionCurrent);
auto smodel = ui->packageView->selectionModel();
connect(smodel, &QItemSelectionModel::currentChanged, this, &VersionPage::packageCurrent);
@ -132,6 +134,7 @@ VersionPage::VersionPage(MinecraftInstance *inst, QWidget *parent)
updateVersionControls();
preselect(0);
connect(m_inst, &BaseInstance::runningStatusChanged, this, &VersionPage::updateRunningStatus);
connect(ui->packageView, &ModListView::customContextMenuRequested, this, &VersionPage::ShowContextMenu);
}
VersionPage::~VersionPage()
@ -139,6 +142,13 @@ VersionPage::~VersionPage()
delete ui;
}
void VersionPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->packageView->mapToGlobal(pos));
delete menu;
}
void VersionPage::packageCurrent(const QModelIndex &current, const QModelIndex &previous)
{
if (!current.isValid())

View File

@ -95,5 +95,5 @@ private slots:
void updateRunningStatus(bool running);
void onGameUpdateError(QString error);
void packageCurrent(const QModelIndex &current, const QModelIndex &previous);
void ShowContextMenu(const QPoint &pos);
};

View File

@ -45,6 +45,8 @@ WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worl
ui->worldTreeView->setSortingEnabled(true);
ui->worldTreeView->setModel(proxy);
ui->worldTreeView->installEventFilter(this);
ui->worldTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->worldTreeView, &QTreeView::customContextMenuRequested, this, &WorldListPage::ShowContextMenu);
auto head = ui->worldTreeView->header();
head->setSectionResizeMode(0, QHeaderView::Stretch);
@ -70,6 +72,13 @@ WorldListPage::~WorldListPage()
delete ui;
}
void WorldListPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->worldTreeView->mapToGlobal(pos));
delete menu;
}
QMenu * WorldListPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();

View File

@ -92,4 +92,6 @@ private slots:
void on_actionView_Folder_triggered();
void worldChanged(const QModelIndex &current, const QModelIndex &previous);
void mceditState(LoggedProcess::State state);
void ShowContextMenu(const QPoint &pos);
};

View File

@ -1,5 +1,6 @@
#include "WideBar.h"
#include <QToolButton>
#include <QMenu>
class ActionButton : public QToolButton
{
@ -20,6 +21,7 @@ private slots:
setIcon(m_action->icon());
setToolTip(m_action->toolTip());
setHidden(!m_action->isVisible());
setFocusPolicy(Qt::NoFocus);
}
private:
QAction * m_action;
@ -38,22 +40,77 @@ WideBar::WideBar(QWidget* parent) : QToolBar(parent)
setMovable(false);
}
struct WideBar::BarEntry {
enum Type {
None,
Action,
Separator,
Spacer
} type = None;
QAction *qAction = nullptr;
QAction *wideAction = nullptr;
};
WideBar::~WideBar()
{
for(auto *iter: m_entries) {
delete iter;
}
}
void WideBar::addAction(QAction* action)
{
auto actionButton = new ActionButton(action, this);
auto newAction = addWidget(actionButton);
m_actionMap[action] = newAction;
auto entry = new BarEntry();
entry->qAction = addWidget(new ActionButton(action, this));
entry->wideAction = action;
entry->type = BarEntry::Action;
m_entries.push_back(entry);
}
void WideBar::addSeparator()
{
auto entry = new BarEntry();
entry->qAction = QToolBar::addSeparator();
entry->type = BarEntry::Separator;
m_entries.push_back(entry);
}
void WideBar::insertSpacer(QAction* action)
{
if(!m_actionMap.contains(action)) {
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [action](BarEntry * entry) {
return entry->wideAction == action;
});
if(iter == m_entries.end()) {
return;
}
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
insertWidget(m_actionMap[action], spacer);
auto entry = new BarEntry();
entry->qAction = insertWidget((*iter)->qAction, spacer);
entry->type = BarEntry::Spacer;
m_entries.insert(iter, entry);
}
QMenu * WideBar::createContextMenu(QWidget *parent, const QString & title)
{
QMenu *contextMenu = new QMenu(title, parent);
for(auto & item: m_entries) {
switch(item->type) {
default:
case BarEntry::None:
break;
case BarEntry::Separator:
case BarEntry::Spacer:
contextMenu->addSeparator();
break;
case BarEntry::Action:
contextMenu->addAction(item->wideAction);
break;
}
}
return contextMenu;
}
#include "WideBar.moc"

View File

@ -4,6 +4,8 @@
#include <QAction>
#include <QMap>
class QMenu;
class WideBar : public QToolBar
{
Q_OBJECT
@ -11,10 +13,14 @@ class WideBar : public QToolBar
public:
explicit WideBar(const QString &title, QWidget * parent = nullptr);
explicit WideBar(QWidget * parent = nullptr);
virtual ~WideBar();
void addAction(QAction *action);
void addSeparator();
void insertSpacer(QAction *action);
QMenu *createContextMenu(QWidget *parent = nullptr, const QString & title = QString());
private:
QMap<QAction *, QAction *> m_actionMap;
struct BarEntry;
QList<BarEntry *> m_entries;
};