From ff81cb215f2cd4d9ecf3bba11ce2b84a248abe8f Mon Sep 17 00:00:00 2001 From: Joris Goosen Date: Wed, 15 Nov 2023 15:38:46 +0100 Subject: [PATCH] FileMenu now dynamically determines the required size for the buttons This makes it smaller than it was before which makes the preference menus *just* usable on the minimumwindow size I did move the treshold spinbox down a bit to make it fit --- .../JASP/Widgets/FileMenu/FileMenu.qml | 72 +++++++++---------- .../JASP/Widgets/FileMenu/PrefsData.qml | 12 ++-- Desktop/mainwindow.cpp | 6 +- Desktop/widgets/filemenu/actionbuttons.cpp | 40 +++++++++-- Desktop/widgets/filemenu/actionbuttons.h | 16 +++-- Desktop/widgets/filemenu/resourcebuttons.cpp | 31 ++++++-- Desktop/widgets/filemenu/resourcebuttons.h | 14 +++- 7 files changed, 126 insertions(+), 65 deletions(-) diff --git a/Desktop/components/JASP/Widgets/FileMenu/FileMenu.qml b/Desktop/components/JASP/Widgets/FileMenu/FileMenu.qml index b2514190b1..e16baeae17 100644 --- a/Desktop/components/JASP/Widgets/FileMenu/FileMenu.qml +++ b/Desktop/components/JASP/Widgets/FileMenu/FileMenu.qml @@ -33,8 +33,6 @@ FocusScope property int actionButtionHeight: 35 * preferencesModel.uiScale property int resourceButtonHeight: 1.5 * actionButtionHeight - property int nbColumns: 1 + (resourceRepeaterId.count > 0 ? 1 : 0 ) - property int colWidths: 190 Connections @@ -54,7 +52,7 @@ FocusScope property real desiredX: !fileMenuModel.visible ? -(resourceScreen.otherColumnsWidth + resourceScreen.width) : 0 x: desiredX - width: (fileMenu.nbColumns * fileMenu.colWidths * preferencesModel.uiScale) + (resourceScreen.aButtonVisible ? resourceScreen.width : 0) + width: (actionMenu.width + resourceMenu.width ) + (resourceScreen.aButtonVisible ? resourceScreen.width : 0) height: fileMenu.height Behavior on x @@ -82,7 +80,7 @@ FocusScope { id: actionMenu anchors.left: parent.left - width: fileMenu.colWidths * preferencesModel.uiScale + width: fileMenuModel.actionButtons.width + jaspTheme.subMenuIconHeight + jaspTheme.generalAnchorMargin * 5 height: parent.height z: 4 contentWidth: width @@ -95,15 +93,14 @@ FocusScope Column { - id: fileAction - spacing: 4 - width: parent.width - jaspTheme.generalAnchorMargin + id: fileAction + spacing: jaspTheme.menuSpacing + width: parent.width anchors { - top: parent.top - topMargin: 5 - horizontalCenter: parent.horizontalCenter + fill: parent + margins: jaspTheme.generalAnchorMargin } Repeater @@ -114,7 +111,7 @@ FocusScope Item { id: itemActionMenu - width: parent.width - (6 * preferencesModel.uiScale) + width: parent.width anchors.left: parent.left height: actionButtionHeight + actionToolSeperator.height enabled: enabledRole @@ -123,18 +120,12 @@ FocusScope { id: actionMenuButton hasSubMenu: hasSubMenuRole - width: itemActionMenu.width + width: parent.width height: actionButtionHeight text: nameRole selected: selectedRole focus: selectedRole - anchors - { - leftMargin: 3 * preferencesModel.uiScale - left: itemActionMenu.left - } - ALTNavigation.enabled: true ALTNavigation.x: width * 0.9 ALTNavigation.onTagMatch: { clicked(); } @@ -165,10 +156,15 @@ FocusScope ToolSeparator { id: actionToolSeperator - anchors.top: actionMenuButton.bottom - width: actionMenuButton.width - anchors.topMargin: (showToolSeperator(typeRole) ? 3 : 0) * preferencesModel.uiScale - anchors.left: actionMenuButton.left + anchors + { + top: actionMenuButton.bottom + topMargin: (showToolSeperator(typeRole) ? 3 : 0) * preferencesModel.uiScale + left: parent.left + right: parent.right + leftMargin: -jaspTheme.generalAnchorMargin + rightMargin: -jaspTheme.generalAnchorMargin + } orientation: Qt.Horizontal visible: showToolSeperator(typeRole) @@ -192,10 +188,10 @@ FocusScope { id: resourceMenu - width: fileMenu.colWidths * preferencesModel.uiScale + width: !hasButtons ? 0 : fileMenuModel.resourceButtons.width + jaspTheme.subMenuIconHeight + jaspTheme.generalAnchorMargin * 5 height: parent.height anchors.left: actionMenu.right - anchors.leftMargin: hasButtons ? 0 : - fileMenu.colWidths * preferencesModel.uiScale + anchors.leftMargin: hasButtons ? 0 : -width z: 2 contentWidth: width contentHeight: resourceLocation.implicitHeight @@ -204,6 +200,12 @@ FocusScope Keys.onLeftPressed: actionMenu.forceActiveFocus(); Keys.onEscapePressed: actionMenu.forceActiveFocus(); + + + + property bool hasButtons: resourceRepeaterId.count > 0 + + visible: hasButtons Behavior on anchors.leftMargin { @@ -217,20 +219,16 @@ FocusScope } } - - - property bool hasButtons: resourceRepeaterId.count > 0 - - visible: hasButtons - Column { id: resourceLocation - anchors.top: parent.top - anchors.topMargin: 5 * preferencesModel.uiScale - anchors.horizontalCenter: parent.horizontalCenter - width: parent.width - jaspTheme.generalAnchorMargin + anchors + { + fill: parent + margins: jaspTheme.generalAnchorMargin + } + spacing: 6 * preferencesModel.uiScale @@ -243,10 +241,8 @@ FocusScope { id: itemResourceMenu - width: parent.width - (6 * preferencesModel.uiScale) + width: parent.width height: resourceButtonHeight - anchors.leftMargin: 3 * preferencesModel.uiScale - anchors.left: parent.left enabled: enabledRole MenuButton @@ -310,7 +306,7 @@ FocusScope Rectangle { - property real otherColumnsWidth: fileMenu.colWidths * fileMenu.nbColumns * preferencesModel.uiScale + property real otherColumnsWidth: actionMenu.width + resourceMenu.width property bool aButtonVisible: resourceRepeaterId.count > 0 && fileMenuModel.resourceButtons.currentQML !== '' property real desiredWidth: Math.min(mainWindowRoot.width - otherColumnsWidth, 600 * preferencesModel.uiScale) diff --git a/Desktop/components/JASP/Widgets/FileMenu/PrefsData.qml b/Desktop/components/JASP/Widgets/FileMenu/PrefsData.qml index 4e38a82190..b816c8d119 100644 --- a/Desktop/components/JASP/Widgets/FileMenu/PrefsData.qml +++ b/Desktop/components/JASP/Widgets/FileMenu/PrefsData.qml @@ -133,13 +133,13 @@ ScrollView Item //Scale threshold { - height: customThreshold.height - width: customThreshold.width + thresholdScale.width + height: customThreshold.height + thresholdScale.height + width: customThreshold.width CheckBox { id: customThreshold - label: qsTr("Import threshold between Categorical or Scale") + label: qsTr("Import threshold between Categorical or Scale") + ":" checked: preferencesModel.customThresholdScale onCheckedChanged: preferencesModel.customThresholdScale = checked ToolTip.delay: 500 @@ -161,9 +161,9 @@ ScrollView anchors { - left: customThreshold.right - leftMargin: jaspTheme.generalAnchorMargin - verticalCenter: parent.verticalCenter + top: customThreshold.bottom + left: customThreshold.left + leftMargin: jaspTheme.subOptionOffset } } } diff --git a/Desktop/mainwindow.cpp b/Desktop/mainwindow.cpp index 3c963476a0..da102b8ab4 100644 --- a/Desktop/mainwindow.cpp +++ b/Desktop/mainwindow.cpp @@ -473,6 +473,8 @@ void MainWindow::makeConnections() connect(_preferences, &PreferencesModel::developerModeChanged, _analyses, &Analyses::refreshAllAnalyses ); connect(_preferences, &PreferencesModel::currentJaspThemeChanged, this, &MainWindow::setCurrentJaspTheme ); connect(_preferences, &PreferencesModel::currentThemeNameChanged, _resultsJsInterface, &ResultsJsInterface::setThemeCss ); + connect(_preferences, &PreferencesModel::currentThemeNameChanged, _fileMenu, &FileMenu::refresh ); + connect(_preferences, &PreferencesModel::uiScaleChanged, _fileMenu, &FileMenu::refresh ); connect(_preferences, &PreferencesModel::resultFontChanged, _resultsJsInterface, &ResultsJsInterface::setFontFamily ); connect(_preferences, &PreferencesModel::resultFontChanged, _engineSync, &EngineSync::refreshAllPlots ); connect(_preferences, &PreferencesModel::restartAllEngines, _engineSync, &EngineSync::haveYouTriedTurningItOffAndOnAgain ); @@ -647,12 +649,12 @@ void MainWindow::loadQML() connect(_preferences, &PreferencesModel::maxFlickVelocityChanged, keyval.second, &JaspTheme::maxFlickVeloHandler ); } - + _fileMenu->refresh(); //Now that the theme is loaded we can determine the proper width for the buttons in the filemenu Log::log() << "Loading HelpWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/HelpWindow.qml")); Log::log() << "Loading AboutWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/AboutWindow.qml")); Log::log() << "Loading ContactWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/ContactWindow.qml")); - Log::log() << "Loading CommunityWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/CommunityWindow.qml")); + Log::log() << "Loading CommunityWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/CommunityWindow.qml")); Log::log() << "Loading MainWindow" << std::endl; _qml->load(QUrl("qrc:///components/JASP/Widgets/MainWindow.qml")); diff --git a/Desktop/widgets/filemenu/actionbuttons.cpp b/Desktop/widgets/filemenu/actionbuttons.cpp index e96d3b194c..889aa9f8ec 100644 --- a/Desktop/widgets/filemenu/actionbuttons.cpp +++ b/Desktop/widgets/filemenu/actionbuttons.cpp @@ -2,12 +2,11 @@ #include #include #include "log.h" +#include "jasptheme.h" -ActionButtons::ActionButtons(QObject *parent) : QAbstractListModel (parent), - _data() - +ActionButtons::ActionButtons(QObject *parent) : QAbstractListModel (parent) { - loadButtonData(_data); + loadButtonData(); for(size_t i=0; i<_data.size(); i++) _opToIndex[_data[i].operation] = i; @@ -17,9 +16,11 @@ ActionButtons::ActionButtons(QObject *parent) : QAbstractListModel (parent), connect(this, &ActionButtons::buttonClicked, this, &ActionButtons::setSelectedAction); } -void ActionButtons::loadButtonData(std::vector &data) +void ActionButtons::loadButtonData() { - _data = + _width = 0; + + _data = { {FileOperation::Open, tr("Open"), true, {ResourceButtons::Computer, ResourceButtons::OSF, ResourceButtons::Database, ResourceButtons::RecentFiles, ResourceButtons::DataLibrary } }, {FileOperation::Save, tr("Save"), false, {} }, @@ -33,6 +34,18 @@ void ActionButtons::loadButtonData(std::vector &data) {FileOperation::Community, tr("Community"), true, {} }, {FileOperation::About, tr("About"), true, {} } }; + + + if(JaspTheme::currentTheme()) + { + + QFontMetricsF ribbonMetrics(JaspTheme::currentTheme()->fontRibbon()); + + for(const auto & action : _data) + _width = std::max(_width, int(ribbonMetrics.boundingRect(action.name).width())); + + emit widthChanged(); + } } QVariant ActionButtons::data(const QModelIndex &index, int role) const @@ -113,7 +126,7 @@ void ActionButtons::refresh() std::vector savedata = _data; - loadButtonData(_data); + loadButtonData(); for(int i=0 ; i < savedata.size() ; i++) _data[i].enabled = savedata[i].enabled; @@ -158,3 +171,16 @@ void ActionButtons::selectButtonDown() } } } + +int ActionButtons::width() const +{ + return _width; +} + +void ActionButtons::setWidth(int newWidth) +{ + if (_width == newWidth) + return; + _width = newWidth; + emit widthChanged(); +} diff --git a/Desktop/widgets/filemenu/actionbuttons.h b/Desktop/widgets/filemenu/actionbuttons.h index 3dcb25b7f0..ec3dc12809 100644 --- a/Desktop/widgets/filemenu/actionbuttons.h +++ b/Desktop/widgets/filemenu/actionbuttons.h @@ -9,7 +9,8 @@ class ActionButtons : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(FileOperation selectedAction READ selectedAction WRITE setSelectedAction NOTIFY selectedActionChanged) + Q_PROPERTY(FileOperation selectedAction READ selectedAction WRITE setSelectedAction NOTIFY selectedActionChanged) + Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged ) public: enum FileOperation {None = 0, Open, Save, SaveAs, ExportResults, ExportData, SyncData, Close, Preferences, Contact, Community, About}; @@ -34,22 +35,27 @@ class ActionButtons : public QAbstractListModel std::set resourceButtonsForButton(FileOperation button); void refresh(); - + + int width() const; + void setWidth(int newWidth); + signals: void selectedActionChanged( ActionButtons::FileOperation selectedAction); void buttonClicked( ActionButtons::FileOperation selectedAction); - + + void widthChanged(); + public slots: void setEnabled( ActionButtons::FileOperation operation, bool enabledState); void setSelectedAction( ActionButtons::FileOperation selectedAction); - private: - void loadButtonData(std::vector & data); + void loadButtonData(); std::vector _data; std::map _opToIndex; FileOperation _selected = None; + int _width = 0; }; #endif // ACTIONBUTTONS_H diff --git a/Desktop/widgets/filemenu/resourcebuttons.cpp b/Desktop/widgets/filemenu/resourcebuttons.cpp index 053eefbe9e..0668abbdde 100644 --- a/Desktop/widgets/filemenu/resourcebuttons.cpp +++ b/Desktop/widgets/filemenu/resourcebuttons.cpp @@ -1,15 +1,16 @@ #include "resourcebuttons.h" +#include "jasptheme.h" ResourceButtons::ResourceButtons(QObject *parent) : QAbstractListModel (parent), _data() { - loadButtonData(_data); + loadButtonData(); for(size_t i=0; i<_data.size(); i++) _buttonToIndex[_data[i].button] = i; } -void ResourceButtons::loadButtonData(std::vector &data) +void ResourceButtons::loadButtonData() { _data = { {ButtonType::RecentFiles, tr("Recent Files"), false, "./RecentFiles.qml" , true}, @@ -23,7 +24,6 @@ void ResourceButtons::loadButtonData(std::vector &data {ButtonType::PrefsUI, tr("Interface"), false, "./PrefsUI.qml" , true}, {ButtonType::PrefsAdvanced, tr("Advanced"), false, "./PrefsAdvanced.qml" , true} }; - } QVariant ResourceButtons::data(const QModelIndex &index, int role) const @@ -197,14 +197,37 @@ void ResourceButtons::refresh() std::vector savedata = _data; - loadButtonData(_data); + loadButtonData(); for(int i=0 ; i < savedata.size() ; i++) { _data[i].enabled = savedata[i].enabled; _data[i].visible = savedata[i].visible; } + + if(JaspTheme::currentTheme()) + { + _width = 0; + QFontMetricsF ribbonMetrics(JaspTheme::currentTheme()->fontRibbon()); + + for(const auto & resource : _data) + _width = std::max(_width, int(ribbonMetrics.boundingRect(resource.name).width())); + + emit widthChanged(); + } endResetModel(); +} +int ResourceButtons::width() const +{ + return _width; +} + +void ResourceButtons::setWidth(int newWidth) +{ + if (_width == newWidth) + return; + _width = newWidth; + emit widthChanged(); } diff --git a/Desktop/widgets/filemenu/resourcebuttons.h b/Desktop/widgets/filemenu/resourcebuttons.h index 25cf79d69d..504e4f25fa 100644 --- a/Desktop/widgets/filemenu/resourcebuttons.h +++ b/Desktop/widgets/filemenu/resourcebuttons.h @@ -12,6 +12,7 @@ class ResourceButtons : public QAbstractListModel Q_OBJECT Q_PROPERTY(QString currentQML READ currentQML WRITE setCurrentQML NOTIFY currentQMLChanged ) Q_PROPERTY(ButtonType selectedButton READ selectedButton WRITE setSelectedButton NOTIFY selectedButtonChanged) + Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged ) public: //["Recent Files", "Current File", "Computer", "OSF", "Data Library"] @@ -42,22 +43,29 @@ class ResourceButtons : public QAbstractListModel Q_INVOKABLE void selectButtonUp(); Q_INVOKABLE void selectButtonDown(); void refresh(); - + + int width() const; + void setWidth(int newWidth); + signals: void currentQMLChanged(QString currentQML); void selectedButtonChanged(ResourceButtons::ButtonType selectedButton); - + + void widthChanged(); + public slots: void setVisible(ResourceButtons::ButtonType button, bool visibility); void setCurrentQML(QString currentQML); void setSelectedButton(ResourceButtons::ButtonType selectedButton); private: - void loadButtonData(std::vector & data); + void loadButtonData(); + std::vector _data; std::map _buttonToIndex; QString _currentQML; ButtonType _selectedButton = None; + int _width; }; #endif // ResourceButtons_H