فهرست منبع

try to adjust VNotebookSelector's popup's width to the content

Le Tan 8 سال پیش
والد
کامیت
ee4bfb171a
2فایلهای تغییر یافته به همراه24 افزوده شده و 0 حذف شده
  1. 23 0
      src/vnotebookselector.cpp
  2. 1 0
      src/vnotebookselector.h

+ 23 - 0
src/vnotebookselector.cpp

@@ -4,6 +4,8 @@
 #include <QListWidget>
 #include <QAction>
 #include <QMenu>
+#include <QGuiApplication>
+#include <QScreen>
 #include "vnotebook.h"
 #include "vconfigmanager.h"
 #include "dialog/vnewnotebookdialog.h"
@@ -361,5 +363,26 @@ void VNotebookSelector::showPopup()
         newNotebook();
         return;
     }
+    resizeListWidgetToContent();
     QComboBox::showPopup();
 }
+
+void VNotebookSelector::resizeListWidgetToContent()
+{
+    static QRect screenRect = QGuiApplication::primaryScreen()->geometry();
+    static int maxMinWidth = screenRect.width() < 400 ? screenRect.width() : screenRect.width() / 2;
+    static int maxMinHeight = screenRect.height() < 400 ? screenRect.height() : screenRect.height() / 2;
+
+    int minWidth = 0;
+    int minHeight = 0;
+    if (m_listWidget->count() > 0) {
+        // Width
+        minWidth = m_listWidget->sizeHintForColumn(0);
+        minWidth = qMin(minWidth, maxMinWidth);
+
+        // Height
+        minHeight = m_listWidget->sizeHintForRow(0) * m_listWidget->count() + 10;
+        minHeight = qMin(minHeight, maxMinHeight);
+    }
+    m_listWidget->setMinimumSize(minWidth, minHeight);
+}

+ 1 - 0
src/vnotebookselector.h

@@ -61,6 +61,7 @@ private:
     int indexOfListItem(const QListWidgetItem *p_item);
     // @p_index is the idnex of QComboBox.
     inline VNotebook *getNotebookFromComboIndex(int p_index);
+    void resizeListWidgetToContent();
 
     VNote *m_vnote;
     QVector<VNotebook *> &m_notebooks;