Jelajahi Sumber

fix combobox style on macOS (#1638)

* fix combobox style on macOS

* fix SingleInstanceGuard on *nix

* fix style on macos

* fix demo
Le Tan 5 tahun lalu
induk
melakukan
774930c950

+ 1 - 1
libs/vtextedit

@@ -1 +1 @@
-Subproject commit f06b36a457ec89e85c372905e5b388e30610e803
+Subproject commit 01fb4118f566ff7a71db67af72de1d586a0e9ee5

+ 0 - 18
src/core/singleinstanceguard.cpp

@@ -27,7 +27,6 @@ bool SingleInstanceGuard::tryRun()
 {
     Q_ASSERT(!m_online);
 
-#if defined(Q_OS_WIN)
     // On Windows, multiple servers on the same name are allowed.
     m_client = tryConnect();
     if (m_client) {
@@ -43,23 +42,6 @@ bool SingleInstanceGuard::tryRun()
         // We still allow the guard to run. There maybe a bug need to fix.
         qWarning() << "failed to connect to an existing instance or establish a new local server";
     }
-#else
-    m_server = tryListen();
-    if (m_server) {
-        // We are the lucky one.
-        qInfo() << "guard succeeds to run";
-    } else {
-        // Here we are sure there is another instance running. But we still use a socket to connect to make sure.
-        m_client = tryConnect();
-        if (m_client) {
-            // We are sure there is another instance running.
-            return false;
-        }
-
-        // We still allow the guard to run. There maybe a bug need to fix.
-        qWarning() << "failed to connect to an existing instance or establish a new local server";
-    }
-#endif
 
     setupServer();
 

+ 13 - 0
src/utils/widgetutils.cpp

@@ -21,6 +21,7 @@
 #include <QFontDatabase>
 #include <QMenu>
 #include <QDebug>
+#include <QFormLayout>
 
 using namespace vnotex;
 
@@ -353,3 +354,15 @@ void WidgetUtils::insertActionAfter(QMenu *p_menu, QAction *p_after, QAction *p_
         p_menu->insertAction(p_action, p_after);
     }
 }
+
+QFormLayout *WidgetUtils::createFormLayout(QWidget *p_parent)
+{
+    auto layout = new QFormLayout(p_parent);
+
+#if defined(Q_OS_MACOS)
+    layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
+    layout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);
+#endif
+
+    return layout;
+}

+ 3 - 0
src/utils/widgetutils.h

@@ -17,6 +17,7 @@ class QScrollArea;
 class QListView;
 class QMenu;
 class QShortcut;
+class QFormLayout;
 
 namespace vnotex
 {
@@ -77,6 +78,8 @@ namespace vnotex
 
         static void insertActionAfter(QMenu *p_menu, QAction *p_after, QAction *p_action);
 
+        static QFormLayout *createFormLayout(QWidget *p_parent = nullptr);
+
     private:
         static void resizeToHideScrollBar(QScrollArea *p_scroll, bool p_vertical, bool p_horizontal);
     };

+ 27 - 0
src/widgets/combobox.cpp

@@ -0,0 +1,27 @@
+#include "combobox.h"
+
+#include <QAbstractItemModel>
+#include <QAbstractItemView>
+
+using namespace vnotex;
+
+ComboBox::ComboBox(QWidget *p_parent)
+    : QComboBox(p_parent)
+{
+}
+
+void ComboBox::showPopup()
+{
+    QComboBox::showPopup();
+
+#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX)
+    auto vw = view();
+    if (count() > 0) {
+        int cnt = qMin(count(), maxVisibleItems());
+        int height = 20 + cnt * vw->visualRect(vw->model()->index(0, 0)).height();
+        if (height > vw->height()) {
+            vw->setMinimumHeight(height);
+        }
+    }
+#endif
+}

+ 18 - 0
src/widgets/combobox.h

@@ -0,0 +1,18 @@
+#ifndef COMBOBOX_H
+#define COMBOBOX_H
+
+#include <QComboBox>
+
+namespace vnotex
+{
+    class ComboBox : public QComboBox
+    {
+        Q_OBJECT
+    public:
+        explicit ComboBox(QWidget *p_parent = nullptr);
+
+        void showPopup() Q_DECL_OVERRIDE;
+    };
+}
+
+#endif // COMBOBOX_H

+ 2 - 1
src/widgets/dialogs/filepropertiesdialog.cpp

@@ -8,6 +8,7 @@
 #include "../lineedit.h"
 #include "../widgetsfactory.h"
 #include <utils/pathutils.h>
+#include <utils/widgetutils.h>
 
 
 using namespace vnotex;
@@ -29,7 +30,7 @@ void FilePropertiesDialog::setupUI()
     auto widget = new QWidget(this);
     setCentralWidget(widget);
 
-    auto mainLayout = new QFormLayout(widget);
+    auto mainLayout = WidgetUtils::createFormLayout(widget);
     mainLayout->setContentsMargins(0, 0, 0, 0);
 
     const QFileInfo info(m_path);

+ 1 - 1
src/widgets/dialogs/folderfilesfilterwidget.cpp

@@ -35,7 +35,7 @@ FolderFilesFilterWidget::FolderFilesFilterWidget(QWidget *p_parent)
 
 void FolderFilesFilterWidget::setupUI()
 {
-    auto mainLayout = new QFormLayout(this);
+    auto mainLayout = WidgetUtils::createFormLayout(this);
     mainLayout->setContentsMargins(0, 0, 0, 0);
 
     {

+ 2 - 1
src/widgets/dialogs/linkinsertdialog.cpp

@@ -6,6 +6,7 @@
 
 #include <widgets/widgetsfactory.h>
 #include <widgets/lineedit.h>
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -29,7 +30,7 @@ void LinkInsertDialog::setupUI(const QString &p_title,
     auto mainWidget = new QWidget(this);
     setCentralWidget(mainWidget);
 
-    auto mainLayout = new QFormLayout(mainWidget);
+    auto mainLayout = WidgetUtils::createFormLayout(mainWidget);
 
     m_linkTextEdit = WidgetsFactory::createLineEdit(p_linkText, mainWidget);
     mainLayout->addRow(tr("&Text:"), m_linkTextEdit);

+ 2 - 1
src/widgets/dialogs/nodeinfowidget.cpp

@@ -8,6 +8,7 @@
 #include <utils/utils.h>
 #include "exception.h"
 #include "nodelabelwithupbutton.h"
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -35,7 +36,7 @@ NodeInfoWidget::NodeInfoWidget(const Node *p_parentNode,
 
 void NodeInfoWidget::setupUI(const Node *p_parentNode)
 {
-    m_mainLayout = new QFormLayout(this);
+    m_mainLayout = WidgetUtils::createFormLayout(this);
 
     m_mainLayout->addRow(tr("Notebook:"),
                          new QLabel(p_parentNode->getNotebook()->getName(), this));

+ 3 - 2
src/widgets/dialogs/notebookinfowidget.cpp

@@ -17,6 +17,7 @@
 #include "configmgr.h"
 #include <utils/pathutils.h>
 #include "exception.h"
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -43,7 +44,7 @@ void NotebookInfoWidget::setupUI()
 QGroupBox *NotebookInfoWidget::setupBasicInfoGroupBox(QWidget *p_parent)
 {
     auto box = new QGroupBox(tr("Basic Information"), p_parent);
-    auto mainLayout = new QFormLayout(box);
+    auto mainLayout = WidgetUtils::createFormLayout(box);
 
     {
         setupNotebookTypeComboBox(box);
@@ -130,7 +131,7 @@ QLayout *NotebookInfoWidget::setupNotebookRootFolderPath(QWidget *p_parent)
 QGroupBox *NotebookInfoWidget::setupAdvancedInfoGroupBox(QWidget *p_parent)
 {
     auto box = new QGroupBox(tr("Advanced Information"), p_parent);
-    auto mainLayout = new QFormLayout(box);
+    auto mainLayout = WidgetUtils::createFormLayout(box);
 
     {
         setupConfigMgrComboBox(box);

+ 2 - 1
src/widgets/dialogs/settings/appearancepage.cpp

@@ -8,6 +8,7 @@
 #include <core/sessionconfig.h>
 #include <core/coreconfig.h>
 #include <core/configmgr.h>
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -19,7 +20,7 @@ AppearancePage::AppearancePage(QWidget *p_parent)
 
 void AppearancePage::setupUI()
 {
-    auto mainLayout = new QFormLayout(this);
+    auto mainLayout = WidgetUtils::createFormLayout(this);
 
     {
         const QString label(tr("System title bar"));

+ 2 - 1
src/widgets/dialogs/settings/editorpage.cpp

@@ -8,6 +8,7 @@
 #include <widgets/widgetsfactory.h>
 #include <core/editorconfig.h>
 #include <core/configmgr.h>
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -19,7 +20,7 @@ EditorPage::EditorPage(QWidget *p_parent)
 
 void EditorPage::setupUI()
 {
-    auto mainLayout = new QFormLayout(this);
+    auto mainLayout = WidgetUtils::createFormLayout(this);
 
     {
         m_autoSavePolicyComboBox = WidgetsFactory::createComboBox(this);

+ 2 - 1
src/widgets/dialogs/settings/generalpage.cpp

@@ -8,6 +8,7 @@
 #include <core/coreconfig.h>
 #include <core/sessionconfig.h>
 #include <core/configmgr.h>
+#include <utils/widgetutils.h>
 
 using namespace vnotex;
 
@@ -19,7 +20,7 @@ GeneralPage::GeneralPage(QWidget *p_parent)
 
 void GeneralPage::setupUI()
 {
-    auto mainLayout = new QFormLayout(this);
+    auto mainLayout = WidgetUtils::createFormLayout(this);
 
     {
         m_localeComboBox = WidgetsFactory::createComboBox(this);

+ 4 - 3
src/widgets/dialogs/settings/markdowneditorpage.cpp

@@ -13,6 +13,7 @@
 #include <core/editorconfig.h>
 #include <core/markdowneditorconfig.h>
 #include <core/configmgr.h>
+#include <utils/widgetutils.h>
 
 #include "editorpage.h"
 
@@ -107,7 +108,7 @@ QString MarkdownEditorPage::title() const
 QGroupBox *MarkdownEditorPage::setupReadGroup()
 {
     auto box = new QGroupBox(tr("Read"), this);
-    auto layout = new QFormLayout(box);
+    auto layout = WidgetUtils::createFormLayout(box);
 
     {
         const QString label(tr("Constrain image width"));
@@ -169,7 +170,7 @@ QGroupBox *MarkdownEditorPage::setupReadGroup()
 QGroupBox *MarkdownEditorPage::setupEditGroup()
 {
     auto box = new QGroupBox(tr("Edit"), this);
-    auto layout = new QFormLayout(box);
+    auto layout = WidgetUtils::createFormLayout(box);
 
     {
         const QString label(tr("Insert file name as title"));
@@ -207,7 +208,7 @@ QGroupBox *MarkdownEditorPage::setupEditGroup()
 QGroupBox *MarkdownEditorPage::setupGeneralGroup()
 {
     auto box = new QGroupBox(tr("General"), this);
-    auto layout = new QFormLayout(box);
+    auto layout = WidgetUtils::createFormLayout(box);
 
     {
         auto sectionLayout = new QHBoxLayout();

+ 2 - 1
src/widgets/dialogs/settings/texteditorpage.cpp

@@ -9,6 +9,7 @@
 #include <core/editorconfig.h>
 #include <core/texteditorconfig.h>
 #include <core/configmgr.h>
+#include <utils/widgetutils.h>
 
 #include "editorpage.h"
 
@@ -22,7 +23,7 @@ TextEditorPage::TextEditorPage(QWidget *p_parent)
 
 void TextEditorPage::setupUI()
 {
-    auto mainLayout = new QFormLayout(this);
+    auto mainLayout = WidgetUtils::createFormLayout(this);
 
     {
         m_lineNumberComboBox = WidgetsFactory::createComboBox(this);

+ 2 - 0
src/widgets/widgets.pri

@@ -2,6 +2,7 @@ SOURCES += \
     $$PWD/attachmentdragdropareaindicator.cpp \
     $$PWD/attachmentpopup.cpp \
     $$PWD/biaction.cpp \
+    $$PWD/combobox.cpp \
     $$PWD/dialogs/dialog.cpp \
     $$PWD/dialogs/filepropertiesdialog.cpp \
     $$PWD/dialogs/imageinsertdialog.cpp \
@@ -82,6 +83,7 @@ HEADERS += \
     $$PWD/attachmentdragdropareaindicator.h \
     $$PWD/attachmentpopup.h \
     $$PWD/biaction.h \
+    $$PWD/combobox.h \
     $$PWD/dialogs/dialog.h \
     $$PWD/dialogs/importfolderutils.h \
     $$PWD/dialogs/filepropertiesdialog.h \

+ 2 - 1
src/widgets/widgetsfactory.cpp

@@ -9,6 +9,7 @@
 #include <QToolButton>
 
 #include "lineedit.h"
+#include "combobox.h"
 
 using namespace vnotex;
 
@@ -38,7 +39,7 @@ QLineEdit *WidgetsFactory::createLineEdit(const QString &p_contents, QWidget *p_
 
 QComboBox *WidgetsFactory::createComboBox(QWidget *p_parent)
 {
-    auto comboBox = new QComboBox(p_parent);
+    auto comboBox = new ComboBox(p_parent);
     auto itemDelegate = new QStyledItemDelegate(comboBox);
     comboBox->setItemDelegate(itemDelegate);
     return comboBox;