Skip to content

Commit

Permalink
App: add action to switch Home/Documents page
Browse files Browse the repository at this point in the history
Relates to GitHub #192
  • Loading branch information
HuguesDelorme committed May 17, 2023
1 parent 71be8b1 commit ccef1f5
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 9 deletions.
21 changes: 19 additions & 2 deletions src/app/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ TaskManager* AppContext::taskMgr() const
return &m_wnd->m_taskMgr;
}

QWidget* AppContext::widgetLeftSidebar() const
{
return m_wnd->m_ui->widget_Left;
}

QWidget* AppContext::widgetMain() const
{
return m_wnd;
}

QWidget* AppContext::widgetLeftSidebar() const
QWidget* AppContext::widgetMainByMode(ModeWidgetMain mode) const
{
return m_wnd->m_ui->widget_Left;
if (mode == ModeWidgetMain::Home)
return m_wnd->m_ui->page_MainHome;
else if (mode == ModeWidgetMain::Documents)
return m_wnd->m_ui->page_MainControl;
else
return nullptr;
}

IAppContext::ModeWidgetMain AppContext::modeWidgetMain() const
Expand All @@ -52,6 +62,13 @@ IAppContext::ModeWidgetMain AppContext::modeWidgetMain() const
return ModeWidgetMain::Unknown;
}

void AppContext::setModeWidgetMain(ModeWidgetMain mode)
{
QWidget* widgetPage = this->widgetMainByMode(mode);
if (widgetPage)
m_wnd->m_ui->stack_Main->setCurrentWidget(widgetPage);
}

V3dViewController* AppContext::v3dViewController(const GuiDocument* guiDoc) const
{
auto widgetDoc = this->findWidgetGuiDocument([=](WidgetGuiDocument* candidate) {
Expand Down
7 changes: 5 additions & 2 deletions src/app/app_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class AppContext : public IAppContext {

GuiApplication* guiApp() const override;
TaskManager* taskMgr() const override;
QWidget* widgetMain() const override;
QWidget* widgetLeftSidebar() const override;
ModeWidgetMain modeWidgetMain() const override;
V3dViewController* v3dViewController(const GuiDocument* guiDoc) const override;

QWidget* widgetMain() const override;
QWidget* widgetMainByMode(ModeWidgetMain mode) const override;
ModeWidgetMain modeWidgetMain() const override;
void setModeWidgetMain(ModeWidgetMain mode) override;

int findDocumentIndex(Document::Identifier docId) const override;
Document::Identifier findDocumentFromIndex(int index) const override;

Expand Down
7 changes: 5 additions & 2 deletions src/app/commands_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ class IAppContext : public QObject {

virtual GuiApplication* guiApp() const = 0;
virtual TaskManager* taskMgr() const = 0;
virtual QWidget* widgetMain() const = 0;
virtual QWidget* widgetLeftSidebar() const = 0;
virtual ModeWidgetMain modeWidgetMain() const = 0;
virtual V3dViewController* v3dViewController(const GuiDocument* guiDoc) const = 0;

virtual QWidget* widgetMain() const = 0;
virtual QWidget* widgetMainByMode(ModeWidgetMain mode) const = 0;
virtual ModeWidgetMain modeWidgetMain() const = 0;
virtual void setModeWidgetMain(ModeWidgetMain mode) = 0;

virtual Document::Identifier currentDocument() const = 0;
virtual void setCurrentDocument(Document::Identifier docId) = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/app/commands_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void CommandRecentFiles::recreateEntries()
menu->clear();
int idFile = 0;
auto appModule = AppModule::get();
const RecentFiles& recentFiles = appModule->properties()->recentFiles.value();
const RecentFiles& recentFiles = appModule->properties()->recentFiles;
for (const RecentFile& recentFile : recentFiles) {
const QString strFilePath = filepathTo<QString>(recentFile.filepath);
const QString strEntryRecentFile = Command::tr("%1 | %2").arg(++idFile).arg(strFilePath);
Expand Down
65 changes: 63 additions & 2 deletions src/app/commands_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ CommandLeftSidebarWidgetToggle::CommandLeftSidebarWidgetToggle(IAppContext* cont

void CommandLeftSidebarWidgetToggle::execute()
{
const bool isVisible = this->context()->widgetLeftSidebar()->isVisible();
this->context()->widgetLeftSidebar()->setVisible(!isVisible);
QWidget* widget = this->context()->widgetLeftSidebar();
widget->setVisible(!widget->isVisible());
}

bool CommandLeftSidebarWidgetToggle::getEnabledStatus() const
Expand Down Expand Up @@ -94,6 +94,67 @@ void CommandLeftSidebarWidgetToggle::updateAction()
this->action()->setToolTip(this->action()->text());
}

CommandSwitchMainWidgetMode::CommandSwitchMainWidgetMode(IAppContext* context)
: Command(context)
{
auto action = new QAction(this);
action->setToolTip(Command::tr("Go To Home Page"));
action->setShortcut(Qt::CTRL + Qt::Key_0);
this->setAction(action);
this->updateAction();
context->widgetMainByMode(IAppContext::ModeWidgetMain::Home)->installEventFilter(this);
context->widgetMainByMode(IAppContext::ModeWidgetMain::Documents)->installEventFilter(this);
}

void CommandSwitchMainWidgetMode::execute()
{
auto newMode = IAppContext::ModeWidgetMain::Unknown;
switch (this->context()->modeWidgetMain()) {
case IAppContext::ModeWidgetMain::Home:
newMode = IAppContext::ModeWidgetMain::Documents;
break;
case IAppContext::ModeWidgetMain::Documents:
newMode = IAppContext::ModeWidgetMain::Home;
break;
}

this->context()->setModeWidgetMain(newMode);
}

bool CommandSwitchMainWidgetMode::getEnabledStatus() const
{
return this->app()->documentCount() != 0;
}

bool CommandSwitchMainWidgetMode::eventFilter(QObject* watched, QEvent* event)
{
if (watched == this->context()->widgetMainByMode(IAppContext::ModeWidgetMain::Home)
|| watched == this->context()->widgetMainByMode(IAppContext::ModeWidgetMain::Documents))
{
if (event->type() == QEvent::Show) {
this->updateAction();
return true;
}
else {
return false;
}
}

return Command::eventFilter(watched, event);
}

void CommandSwitchMainWidgetMode::updateAction()
{
switch (this->context()->modeWidgetMain()) {
case IAppContext::ModeWidgetMain::Home:
this->action()->setText(Command::tr("Go To Documents"));
break;
case IAppContext::ModeWidgetMain::Documents:
this->action()->setText(Command::tr("Go To Home Page"));
break;
}
}

CommandPreviousDocument::CommandPreviousDocument(IAppContext* context)
: Command(context)
{
Expand Down
12 changes: 12 additions & 0 deletions src/app/commands_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class CommandLeftSidebarWidgetToggle : public Command {
void updateAction();
};

class CommandSwitchMainWidgetMode : public Command {
public:
CommandSwitchMainWidgetMode(IAppContext* context);
void execute() override;
bool getEnabledStatus() const override;

bool eventFilter(QObject* watched, QEvent* event) override;

private:
void updateAction();
};

class CommandPreviousDocument : public Command {
public:
CommandPreviousDocument(IAppContext* context);
Expand Down
2 changes: 2 additions & 0 deletions src/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ void MainWindow::createCommands()
// "Window" commands
this->addCommand<CommandLeftSidebarWidgetToggle>("toggle-left-sidebar");
this->addCommand<CommandMainWidgetToggleFullscreen>("toggle-fullscreen");
this->addCommand<CommandSwitchMainWidgetMode>("switch-main-widget-mode");
this->addCommand<CommandPreviousDocument>("previous-doc");
this->addCommand<CommandNextDocument>("next-doc");

Expand Down Expand Up @@ -295,6 +296,7 @@ void MainWindow::createMenus()
menu->addAction(fnGetAction("toggle-left-sidebar"));
menu->addAction(fnGetAction("toggle-fullscreen"));
menu->addSeparator();
menu->addAction(fnGetAction("switch-main-widget-mode"));
menu->addAction(fnGetAction("previous-doc"));
menu->addAction(fnGetAction("next-doc"));
}
Expand Down

0 comments on commit ccef1f5

Please sign in to comment.