Explorar o código

Fix the auto resize of ctkSettingsDialog's left panel

For some reason, QTreeView hides sizeHintForColumn from API.
That was the reason why QAbstractItemView::sizeHintForColumn was called
instead.
However here we really want the QTreeView column sizeHint and not the
abstract view sizeHint because we want to take the children width into
account.
Closes #234
Julien Finet %!s(int64=12) %!d(string=hai) anos
pai
achega
b93ca60990

+ 1 - 1
Libs/Widgets/Testing/Cpp/ctkSettingsDialogTest1.cpp

@@ -78,7 +78,7 @@ int ctkSettingsDialogTest1(int argc, char * argv [] )
   settingsDialog.addPanel("Panel 2", new ctkSettingsPanel);
   settingsDialog.addPanel("Panel 3", new ctkSettingsPanel);
   ctkSettingsPanel* panel4 = new ctkSettingsPanel;
-  settingsDialog.addPanel("Panel 4", panel4, panel1);
+  settingsDialog.addPanel("Panel 4 with long title", panel4, panel1);
 
   QCheckBox* box = new QCheckBox(panel4);
   box->setChecked(false); // false by default but we just want to make sure

+ 8 - 3
Libs/Widgets/ctkSettingsDialog.cpp

@@ -90,6 +90,12 @@ void ctkSettingsDialogPrivate::init()
   QObject::connect(this->SettingsTreeWidget,
     SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
     q, SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
+
+  QObject::connect(this->SettingsTreeWidget, SIGNAL(expanded(QModelIndex)),
+    q, SLOT(adjustTreeWidgetToContents()));
+  QObject::connect(this->SettingsTreeWidget, SIGNAL(collapsed(QModelIndex)),
+    q, SLOT(adjustTreeWidgetToContents()));
+
   QObject::connect(this->SettingsButtonBox, SIGNAL(clicked(QAbstractButton*)),
                    q, SLOT(onDialogButtonClicked(QAbstractButton*)));
 
@@ -408,11 +414,10 @@ void ctkSettingsDialog::adjustTreeWidgetToContents()
   Q_D(const ctkSettingsDialog);
 
   d->SettingsTreeWidget->resizeColumnToContents(0);
-
   d->SettingsTreeWidget->setFixedWidth(
-      d->SettingsTreeWidget->QAbstractItemView::sizeHintForColumn(0) +
+      qobject_cast<QAbstractItemView*>(d->SettingsTreeWidget)->sizeHintForColumn(0) +
       d->SettingsTreeWidget->fontMetrics().width('*') +
-      2 * d->SettingsTreeWidget->indentation() +
+      d->SettingsTreeWidget->indentation() / 2+
       2 * d->SettingsTreeWidget->frameWidth());
 }
 

+ 4 - 3
Libs/Widgets/ctkSettingsDialog.h

@@ -64,8 +64,6 @@ public:
   void addPanel(const QString& label, ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel = 0);
   void addPanel(const QString& label, const QIcon& icon, ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel = 0);
 
-  void adjustTreeWidgetToContents();
-
   bool resetButton()const;
   void setResetButton(bool show);
 
@@ -81,10 +79,13 @@ public Q_SLOTS:
   void applySettings();
   void resetSettings();
   void restoreDefaultSettings();
-  
+
   virtual void accept();
   virtual void reject();
 
+  /// Resize the left panel based on the panels titles.
+  void adjustTreeWidgetToContents();
+
 Q_SIGNALS:
   void settingChanged(const QString& key, const QVariant& value);
   /// Signal fired when the user accepts to restart the application because