From 7a6807b001eb0d25b3924045a2b8acdf27eb8ae3 Mon Sep 17 00:00:00 2001 From: shun-iwasawa Date: Aug 02 2019 10:18:00 +0000 Subject: Modify saving and restoring panels / popups geometries. (#2685) * restore floating panel state * save popup geometry in dtor --- diff --git a/toonz/sources/include/toonzqt/dvdialog.h b/toonz/sources/include/toonzqt/dvdialog.h index 8f1f49c..f745ca6 100644 --- a/toonz/sources/include/toonzqt/dvdialog.h +++ b/toonz/sources/include/toonzqt/dvdialog.h @@ -170,7 +170,6 @@ protected: class DVAPI Dialog : public QDialog { Q_OBJECT - static QSettings *m_settings; // If the dialog has button then is modal too. bool m_hasButton; QString m_name; @@ -183,7 +182,6 @@ protected: QHBoxLayout *m_buttonLayout; QList m_labelList; void resizeEvent(QResizeEvent *e) override; - void moveEvent(QMoveEvent *e) override; public: QVBoxLayout *m_topLayout; diff --git a/toonz/sources/toonz/floatingpanelcommand.cpp b/toonz/sources/toonz/floatingpanelcommand.cpp index 2e25ca0..26e2f20 100644 --- a/toonz/sources/toonz/floatingpanelcommand.cpp +++ b/toonz/sources/toonz/floatingpanelcommand.cpp @@ -137,6 +137,7 @@ TPanel *OpenFloatingPanel::getOrOpenFloatingPanel( QString::fromStdString(panelType)); if (!panel) return 0; // it should never happen // panel->setWindowTitle(QObject::tr(m_title.toStdString().c_str())); + panel->restoreFloatingPanelState(); panel->setFloating(true); panel->show(); panel->raise(); diff --git a/toonz/sources/toonz/pane.cpp b/toonz/sources/toonz/pane.cpp index 527644a..e3d08a2 100644 --- a/toonz/sources/toonz/pane.cpp +++ b/toonz/sources/toonz/pane.cpp @@ -6,6 +6,7 @@ #include "tapp.h" #include "mainwindow.h" #include "tenv.h" +#include "saveloadqsettings.h" #include "toonzqt/gutil.h" @@ -29,6 +30,7 @@ #include #include #include +#include extern TEnv::StringVar EnvSafeAreaName; @@ -60,7 +62,22 @@ TPanel::TPanel(QWidget *parent, Qt::WindowFlags flags, //----------------------------------------------------------------------------- -TPanel::~TPanel() {} +TPanel::~TPanel() { + // On quitting, save the floating panel's geomtry and state in order to + // restore them when opening the floating panel next time + if (isFloating()) { + TFilePath savePath = + ToonzFolder::getMyModuleDir() + TFilePath("popups.ini"); + QSettings settings(QString::fromStdWString(savePath.getWideString()), + QSettings::IniFormat); + settings.beginGroup("Panels"); + settings.beginGroup(QString::fromStdString(m_panelType)); + settings.setValue("geometry", geometry()); + if (SaveLoadQSettings *persistent = + dynamic_cast(widget())) + persistent->save(settings); + } +} //----------------------------------------------------------------------------- @@ -104,7 +121,7 @@ void TPanel::onCloseButtonPressed() { //----------------------------------------------------------------------------- /*! activate the panel and set focus specified widget when mouse enters -*/ + */ void TPanel::enterEvent(QEvent *event) { // Only when Toonz application is active if (qApp->activeWindow()) { @@ -120,9 +137,35 @@ void TPanel::enterEvent(QEvent *event) { //----------------------------------------------------------------------------- /*! clear focus when mouse leaves -*/ + */ void TPanel::leaveEvent(QEvent *event) { widgetClearFocusOnLeave(); } +//----------------------------------------------------------------------------- +/*! load and restore previous geometry and state of the floating panel. + called from the function OpenFloatingPanel::getOrOpenFloatingPanel() + in floatingpanelcommand.cpp +*/ +void TPanel::restoreFloatingPanelState() { + TFilePath savePath = ToonzFolder::getMyModuleDir() + TFilePath("popups.ini"); + QSettings settings(QString::fromStdWString(savePath.getWideString()), + QSettings::IniFormat); + settings.beginGroup("Panels"); + + if (!settings.childGroups().contains(QString::fromStdString(m_panelType))) + return; + + settings.beginGroup(QString::fromStdString(m_panelType)); + + QRect geom = settings.value("geometry", saveGeometry()).toRect(); + // check if it can be visible in the current screen + if (!(geom & QApplication::desktop()->availableGeometry(this)).isEmpty()) + setGeometry(geom); + // load optional settings + if (SaveLoadQSettings *persistent = + dynamic_cast(widget())) + persistent->load(settings); +} + //============================================================================= // TPanelTitleBarButton //----------------------------------------------------------------------------- @@ -181,9 +224,10 @@ void TPanelTitleBarButton::setPressed(bool pressed) { void TPanelTitleBarButton::paintEvent(QPaintEvent *event) { QPainter painter(this); - painter.drawPixmap( - 0, 0, m_pressed ? m_pressedPixmap : m_rollover ? m_rolloverPixmap - : m_standardPixmap); + painter.drawPixmap(0, 0, + m_pressed + ? m_pressedPixmap + : m_rollover ? m_rolloverPixmap : m_standardPixmap); painter.end(); } diff --git a/toonz/sources/toonz/pane.h b/toonz/sources/toonz/pane.h index 33f1697..333cd3d 100644 --- a/toonz/sources/toonz/pane.h +++ b/toonz/sources/toonz/pane.h @@ -55,7 +55,7 @@ signals: //----------------------------------------------------------------------------- /*! specialized button for sage area which enables to choose safe area size by * context menu -*/ + */ class TPanelTitleBarButtonForSafeArea final : public TPanelTitleBarButton { Q_OBJECT @@ -232,6 +232,8 @@ public: return false; }; + void restoreFloatingPanelState(); + protected: void paintEvent(QPaintEvent *) override; void enterEvent(QEvent *) override; diff --git a/toonz/sources/toonzqt/dvdialog.cpp b/toonz/sources/toonzqt/dvdialog.cpp index ca87760..76ca66c 100644 --- a/toonz/sources/toonzqt/dvdialog.cpp +++ b/toonz/sources/toonzqt/dvdialog.cpp @@ -210,7 +210,10 @@ clearButtonBar(). You can also setButtonBarSpacing() functions. */ //----------------------------------------------------------------------------- -QSettings *Dialog::m_settings = 0; + +namespace { +QString settingsPath; +}; Dialog::Dialog(QWidget *parent, bool hasButton, bool hasFixedSize, const QString &name) @@ -274,17 +277,17 @@ Dialog::Dialog(QWidget *parent, bool hasButton, bool hasFixedSize, setWindowFlags(Qt::Tool); #endif - if (!m_settings) { - TFilePath layoutDir = ToonzFolder::getMyModuleDir(); - TFilePath savePath = layoutDir + TFilePath("popups.ini"); - m_settings = - new QSettings(QString::fromStdWString(savePath.getWideString()), - QSettings::IniFormat); + if (settingsPath.isEmpty()) { + TFilePath savePath = + ToonzFolder::getMyModuleDir() + TFilePath("popups.ini"); + settingsPath = QString::fromStdWString(savePath.getWideString()); } + QSettings settings(settingsPath, QSettings::IniFormat); + if (name == QString()) return; m_name = name + "DialogGeometry"; - QString geo = m_settings->value(m_name).toString(); + QString geo = settings.value(m_name).toString(); if (geo != QString()) { QStringList values = geo.split(" "); assert(values.size() == 4); @@ -312,31 +315,28 @@ Dialog::Dialog(QWidget *parent, bool hasButton, bool hasFixedSize, // on another monitor by default, but this is better than // a user thinking the program is broken because they didn't notice // the popup on another monitor - if (x > screen.right() - 50) x = screen.right() - 50; - if (x < screen.left()) x = screen.left(); + if (x > screen.right() - 50) x = screen.right() - 50; + if (x < screen.left()) x = screen.left(); if (y > screen.bottom() - 90) y = screen.bottom() - 90; - if (y < screen.top()) y = screen.top(); + if (y < screen.top()) y = screen.top(); setGeometry(x, y, values.at(2).toInt(), values.at(3).toInt()); - m_settings->setValue(m_name, - QString::number(x) + " " + QString::number(y) + " " + - QString::number(values.at(2).toInt()) + " " + - QString::number(values.at(3).toInt())); + settings.setValue(m_name, QString::number(x) + " " + QString::number(y) + + " " + QString::number(values.at(2).toInt()) + + " " + QString::number(values.at(3).toInt())); } } //----------------------------------------------------------------------------- -Dialog::~Dialog() {} -//----------------------------------------------------------------------------- - -void Dialog::moveEvent(QMoveEvent *e) { +Dialog::~Dialog() { if (m_name == QString()) return; QRect r = geometry(); - m_settings->setValue(m_name, QString::number(r.left()) + " " + - QString::number(r.top()) + " " + - QString::number(r.width()) + " " + - QString::number(r.height())); + QSettings settings(settingsPath, QSettings::IniFormat); + settings.setValue(m_name, QString::number(r.left()) + " " + + QString::number(r.top()) + " " + + QString::number(r.width()) + " " + + QString::number(r.height())); } //--------------------------------------------------------------------------------- @@ -344,17 +344,8 @@ void Dialog::moveEvent(QMoveEvent *e) { void Dialog::resizeEvent(QResizeEvent *e) { if (Preferences::instance()->getCurrentLanguage() != "English") { QSize t = this->size(); - for (QLabel *s : m_labelList) - s->setFixedWidth(t.width() * .35); + for (QLabel *s : m_labelList) s->setFixedWidth(t.width() * .35); } - - if (m_name == QString()) return; - - QRect r = geometry(); - m_settings->setValue(m_name, QString::number(r.left()) + " " + - QString::number(r.top()) + " " + - QString::number(r.width()) + " " + - QString::number(r.height())); } //----------------------------------------------------------------------------- @@ -380,23 +371,24 @@ void Dialog::hideEvent(QHideEvent *event) { } QRect screen = QApplication::desktop()->availableGeometry(currentScreen); - if (x > screen.right() - 50) x = screen.right() - 50; - if (x < screen.left()) x = screen.left(); + if (x > screen.right() - 50) x = screen.right() - 50; + if (x < screen.left()) x = screen.left(); if (y > screen.bottom() - 90) y = screen.bottom() - 90; - if (y < screen.top()) y = screen.top(); + if (y < screen.top()) y = screen.top(); move(QPoint(x, y)); resize(size()); QRect r = geometry(); - m_settings->setValue(m_name, QString::number(r.left()) + " " + - QString::number(r.top()) + " " + - QString::number(r.width()) + " " + - QString::number(r.height())); + QSettings settings(settingsPath, QSettings::IniFormat); + settings.setValue(m_name, QString::number(r.left()) + " " + + QString::number(r.top()) + " " + + QString::number(r.width()) + " " + + QString::number(r.height())); emit dialogClosed(); } //----------------------------------------------------------------------------- /*! Create the new layouts (2 Vertical) for main part of dialog. -*/ + */ void Dialog::beginVLayout() { m_isMainVLayout = true; @@ -435,7 +427,7 @@ void Dialog::endVLayout() { //----------------------------------------------------------------------------- /*! Create a new Horizontal Layout for main part of dialog. -*/ + */ void Dialog::beginHLayout() { m_isMainHLayout = true; m_mainHLayout = new QHBoxLayout; @@ -445,7 +437,7 @@ void Dialog::beginHLayout() { //----------------------------------------------------------------------------- /*! Add to main part of dialog the Horizontal Layout and set it to 0. -*/ + */ void Dialog::endHLayout() { m_isMainHLayout = false; if (!m_mainHLayout) return; @@ -647,7 +639,7 @@ void Dialog::addLayouts(QLayout *firstL, QLayout *secondL) { //----------------------------------------------------------------------------- /*! Add spacing \b spacing to main part of dialog. -*/ + */ void Dialog::addSpacing(int spacing) { if (m_isMainVLayout) { assert(m_leftVLayout && m_rightVLayout); @@ -686,14 +678,14 @@ void Dialog::addSeparator(QString name) { //----------------------------------------------------------------------------- /*! Set the alignement of the main layout -*/ + */ void Dialog::setAlignment(Qt::Alignment alignment) { m_mainFrame->layout()->setAlignment(alignment); } //----------------------------------------------------------------------------- /*! Set to \b spacing spacing of main part of dialog. -*/ + */ void Dialog::setTopSpacing(int spacing) { m_layoutSpacing = spacing; m_topLayout->setSpacing(spacing); @@ -718,26 +710,26 @@ int Dialog::getLayoutInsertedSpacing() { return m_layoutSpacing; } //----------------------------------------------------------------------------- /*! Set to \b margin margin of main part of dialog. -*/ + */ void Dialog::setTopMargin(int margin) { m_topLayout->setMargin(margin); } //----------------------------------------------------------------------------- /*! Set to \b margin margin of button part of dialog. -*/ + */ void Dialog::setButtonBarMargin(int margin) { m_buttonLayout->setMargin(margin); } //----------------------------------------------------------------------------- /*! Set to \b spacing spacing of button part of dialog. -*/ + */ void Dialog::setButtonBarSpacing(int spacing) { m_buttonLayout->setSpacing(spacing); } //----------------------------------------------------------------------------- /*! Add a widget to the button part of dialog. -*/ + */ void Dialog::addButtonBarWidget(QWidget *widget) { widget->setMinimumSize(65, 25); assert(m_hasButton); @@ -749,7 +741,7 @@ void Dialog::addButtonBarWidget(QWidget *widget) { //----------------------------------------------------------------------------- /*! Remove all widget from the button part of dialog. -*/ + */ void Dialog::clearButtonBar() { for (int i = 0; i < (int)m_buttonBarWidgets.size(); i++) { m_buttonLayout->removeWidget(m_buttonBarWidgets[i]); @@ -759,7 +751,7 @@ void Dialog::clearButtonBar() { //----------------------------------------------------------------------------- /*! Add two widget to the button part of dialog. -*/ + */ void Dialog::addButtonBarWidget(QWidget *first, QWidget *second) { first->setMinimumSize(65, 25); second->setMinimumSize(65, 25); @@ -772,7 +764,7 @@ void Dialog::addButtonBarWidget(QWidget *first, QWidget *second) { //----------------------------------------------------------------------------- /*! Add three widget to the button part of dialog. -*/ + */ void Dialog::addButtonBarWidget(QWidget *first, QWidget *second, QWidget *third) { first->setMinimumSize(65, 25); @@ -788,7 +780,7 @@ void Dialog::addButtonBarWidget(QWidget *first, QWidget *second, //----------------------------------------------------------------------------- /*! Add four widget to the button part of dialog. -*/ + */ void Dialog::addButtonBarWidget(QWidget *first, QWidget *second, QWidget *third, QWidget *fourth) { first->setMinimumSize(65, 25); @@ -1356,7 +1348,7 @@ QString DVGui::getText(const QString &title, const QString &labelText, dialog.addButtonBarWidget(okBtn, cancelBtn); - int ret = dialog.exec(); + int ret = dialog.exec(); if (ok) *ok = (ret == QDialog::Accepted); return nameFld->text(); @@ -1377,7 +1369,7 @@ bool isStyleIdInPalette(int styleId, const TPalette *palette) { } return false; } -} +} // namespace //-----------------------------------------------------------------------------