220 lines
4.9 KiB
C++
220 lines
4.9 KiB
C++
#include "LMainWindow.h"
|
|
#include "PluginContentWidget.h"
|
|
#include "PluginContentWidgetContextBase.h"
|
|
#include "PluginModule.h"
|
|
#include "PluginRegister.h"
|
|
|
|
#include <QDebug>
|
|
#include <QMenuBar>
|
|
#include <QStatusBar>
|
|
#include <QToolBar>
|
|
|
|
namespace LMainWindow_details {
|
|
|
|
class LMainWindowContentContext: public PluginContentWidgetContextBase {
|
|
public:
|
|
explicit LMainWindowContentContext(LMainWindow *window)
|
|
: m_window(window)
|
|
{}
|
|
|
|
void setCaption(PluginContentWidget *content, const QString &caption, const QString &hint = {}) override
|
|
{
|
|
m_window->setTabCaptionForWidget(content, caption, hint);
|
|
}
|
|
|
|
void setIcon(PluginContentWidget *content, const QString &iconname) override
|
|
{
|
|
m_window->setTabIcon(content, iconname);
|
|
}
|
|
|
|
void showStatusMessage(const QString &msg) override
|
|
{
|
|
m_window->statusBar()->showMessage(msg);
|
|
}
|
|
|
|
void addContentWidget(PluginContentWidget *widget) override
|
|
{
|
|
m_window->addPage(widget, "");
|
|
}
|
|
|
|
QWidget* container() override
|
|
{
|
|
return m_window;
|
|
}
|
|
private:
|
|
LMainWindow *m_window;
|
|
};
|
|
|
|
}
|
|
using namespace LMainWindow_details;
|
|
|
|
|
|
LMainWindow::LMainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
m_context = new LMainWindowContentContext(this);
|
|
|
|
m_tabWidget = new QTabWidget(this);
|
|
m_tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
|
|
m_tabWidget->setDocumentMode(true);
|
|
|
|
setCentralWidget(m_tabWidget);
|
|
|
|
// menu
|
|
auto menuBar = new QMenuBar(this);
|
|
setMenuBar(menuBar);
|
|
|
|
// tooolbar
|
|
m_mainToolBar = new QToolBar(this);
|
|
addToolBar(Qt::TopToolBarArea, m_mainToolBar);
|
|
|
|
// statusbar
|
|
auto statusBar = new QStatusBar(this);
|
|
setStatusBar(statusBar);
|
|
|
|
m_fileMenu = new QMenu("File", this);
|
|
|
|
createActions();
|
|
m_mainToolBar->addAction(m_closeAction);
|
|
|
|
connect(m_tabWidget, &QTabWidget::tabCloseRequested, this, &LMainWindow::tabWidget_tabCloseRequested);
|
|
connect(m_tabWidget, &QTabWidget::currentChanged, this, &LMainWindow::tabWidget_currentChanged);
|
|
}
|
|
|
|
LMainWindow::~LMainWindow()
|
|
{
|
|
delete m_context;
|
|
}
|
|
|
|
void LMainWindow::initModuleMenus()
|
|
{
|
|
menuBar()->addMenu(m_fileMenu);
|
|
addModuleMenuActions();
|
|
}
|
|
|
|
void LMainWindow::addModuleMenuActions()
|
|
{
|
|
auto reg = PluginRegister::getInstance();
|
|
auto mods = reg->modules();
|
|
for (auto && mod : mods) {
|
|
auto items = mod.second->menuActions();
|
|
for (auto && item : items) {
|
|
addMenuAction(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
void LMainWindow::createActions()
|
|
{
|
|
{
|
|
auto action = m_closeAction = new QAction(this);
|
|
QIcon icon;
|
|
icon.addFile(QString::fromUtf8(":/icons/page_white_delete.png"), QSize(), QIcon::Normal, QIcon::On);
|
|
action->setIcon(icon);
|
|
connect(m_closeAction, &QAction::triggered, this, &LMainWindow::actionClose_triggered);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void LMainWindow::addMenuAction(const MenuAction &ma)
|
|
{
|
|
qDebug() << "add action " << ma.text();
|
|
//auto ac =
|
|
m_fileMenu->addAction(ma.icon(), ma.text(),
|
|
[ma, this] ()
|
|
{
|
|
ma.perform(m_context);
|
|
},
|
|
ma.shortCut());
|
|
|
|
|
|
// auto ac = new QAction(this);
|
|
// ac->
|
|
}
|
|
|
|
void LMainWindow::actionClose_triggered()
|
|
{
|
|
m_tabWidget->tabCloseRequested(m_tabWidget->currentIndex());
|
|
}
|
|
|
|
|
|
void LMainWindow::addPage(PluginContentWidget* page, QString caption)
|
|
{
|
|
m_tabWidget->addTab(page, caption);
|
|
m_tabWidget->setCurrentWidget(page);
|
|
}
|
|
|
|
void LMainWindow::setTabCaptionForWidget(QWidget *widget, const QString &caption, const QString &hint)
|
|
{
|
|
auto index = m_tabWidget->indexOf(widget);
|
|
m_tabWidget->setTabText(index, caption);
|
|
m_tabWidget->setTabToolTip(index, hint);
|
|
}
|
|
|
|
void LMainWindow::setTabIcon(QWidget *widget, const QString &iconname)
|
|
{
|
|
auto index = m_tabWidget->indexOf(widget);
|
|
auto n = ":/icons/16x16/" + iconname;
|
|
m_tabWidget->setTabIcon(index, QIcon(n));
|
|
}
|
|
|
|
IPluginContentWidgetContext* LMainWindow::context()
|
|
{
|
|
return m_context;
|
|
}
|
|
|
|
void LMainWindow::tabWidget_tabCloseRequested(int index)
|
|
{
|
|
QWidget *widget = m_tabWidget->widget(index);
|
|
PluginContentWidget *plg_page = dynamic_cast<PluginContentWidget*>(widget);
|
|
if (plg_page) {
|
|
if (plg_page->canClose()) {
|
|
m_tabWidget->removeTab(index);
|
|
delete plg_page;
|
|
}
|
|
}
|
|
else {
|
|
// old behaviour shouldn't be needed any more when all pages have been migrated
|
|
// to PlgPage
|
|
m_tabWidget->removeTab(index);
|
|
delete widget;
|
|
}
|
|
}
|
|
|
|
void LMainWindow::tabWidget_currentChanged(int index)
|
|
{
|
|
// remove buttons of old page
|
|
if (m_previousPage) {
|
|
removeToolBarButtonsForPage(m_previousPage);
|
|
}
|
|
|
|
// add buttons of new page
|
|
PluginContentWidget * page = nullptr;
|
|
if (index >= 0) {
|
|
QWidget *widget = m_tabWidget->widget(index);
|
|
page = dynamic_cast<PluginContentWidget*>(widget);
|
|
if (page) {
|
|
addToolBarButtonsForPage(page);
|
|
}
|
|
}
|
|
m_previousPage = page;
|
|
}
|
|
|
|
void LMainWindow::addToolBarButtonsForPage(PluginContentWidget *page)
|
|
{
|
|
std::vector<QAction*> actions = page->getToolbarActions();
|
|
QList<QAction*> list;
|
|
for (auto act : actions) {
|
|
list.append(act);
|
|
}
|
|
m_mainToolBar->addActions(list);
|
|
}
|
|
|
|
void LMainWindow::removeToolBarButtonsForPage(PluginContentWidget *page)
|
|
{
|
|
std::vector<QAction*> actions = page->getToolbarActions();
|
|
for (auto act : actions) {
|
|
m_mainToolBar->removeAction(act);
|
|
}
|
|
}
|