changeset 1287:2dd7f764c3a6 plugin-path-config

Better editing mechanism
author Chris Cannam
date Tue, 15 May 2018 13:56:26 +0100
parents e327bbf4bf57
children 4683b6ffb76a
files widgets/PluginPathConfigurator.cpp widgets/PluginPathConfigurator.h
diffstat 2 files changed, 67 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/widgets/PluginPathConfigurator.cpp	Tue May 15 11:18:07 2018 +0100
+++ b/widgets/PluginPathConfigurator.cpp	Tue May 15 13:56:26 2018 +0100
@@ -15,18 +15,56 @@
 #include "PluginPathConfigurator.h"
 
 #include <QPushButton>
+#include <QListWidget>
 #include <QLabel>
 
 #include "IconLoader.h"
 #include "WidgetScale.h"
 
 PluginPathConfigurator::PluginPathConfigurator(QWidget *parent) :
-    QFrame(parent),
-    m_innerFrame(0)
+    QFrame(parent)
 {
-    setFrameStyle(StyledPanel | Sunken);
     m_layout = new QGridLayout;
     setLayout(m_layout);
+
+    int row = 0;
+    
+    QLabel *header = new QLabel;
+    header->setText(tr("Plugin locations"));
+    m_layout->addWidget(header, row, 0);
+    ++row;
+    
+    m_list = new QListWidget;
+    m_layout->addWidget(m_list, row, 0, 1, 2);
+    m_layout->setRowStretch(row, 10);
+    m_layout->setColumnStretch(0, 10);
+    ++row;
+
+    QHBoxLayout *buttons = new QHBoxLayout;
+    
+    QPushButton *down = new QPushButton;
+    down->setIcon(IconLoader().load("down"));
+    down->setToolTip(tr("Move the selected location down in the list"));
+    down->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
+    connect(down, SIGNAL(clicked()), this, SLOT(downClicked()));
+    buttons->addWidget(down);
+
+    QPushButton *up = new QPushButton;
+    up->setIcon(IconLoader().load("up"));
+    up->setToolTip(tr("Move the selected location up in the list"));
+    up->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
+    connect(up, SIGNAL(clicked()), this, SLOT(upClicked()));
+    buttons->addWidget(up);
+
+    QPushButton *del = new QPushButton;
+    del->setIcon(IconLoader().load("datadelete"));
+    del->setToolTip(tr("Remove the selected location from the list"));
+    del->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
+    connect(del, SIGNAL(clicked()), this, SLOT(deleteClicked()));
+    buttons->addWidget(del);
+
+    m_layout->addLayout(buttons, row, 1);
+    ++row;
 }
 
 PluginPathConfigurator::~PluginPathConfigurator()
@@ -42,74 +80,28 @@
 }
 
 void
-PluginPathConfigurator::populate()
+PluginPathConfigurator::populate(int makeCurrent)
 {
-    delete m_innerFrame;
-    m_innerFrame = new QWidget;
-    m_layout->addWidget(m_innerFrame, 0, 0);
+    m_list->clear();
 
-    QGridLayout *innerLayout = new QGridLayout;
-    m_innerFrame->setLayout(innerLayout);
-    
-    QLabel *header = new QLabel(m_innerFrame);
-    header->setText(QString("<b>%1</b>").arg(tr("Location")));
-    innerLayout->addWidget(header, 0, 3);
-    
     for (int i = 0; i < m_path.size(); ++i) {
+        m_list->addItem(m_path[i]);
+    }
 
-        int col = 0;
-        int row = i + 1;
-        QString dir = m_path[i];
-
-        if (i > 0) {
-            QPushButton *up = new QPushButton(m_innerFrame);
-            up->setObjectName(QString("%1").arg(i));
-            up->setIcon(IconLoader().load("up"));
-            up->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
-            connect(up, SIGNAL(clicked()), this, SLOT(upClicked()));
-            innerLayout->addWidget(up, row, col);
-        }
-        ++col;
-
-        if (i + 1 < m_path.size()) {
-            QPushButton *down = new QPushButton(m_innerFrame);
-            down->setObjectName(QString("%1").arg(i));
-            down->setIcon(IconLoader().load("down"));
-            down->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
-            connect(down, SIGNAL(clicked()), this, SLOT(downClicked()));
-            innerLayout->addWidget(down, row, col);
-        }
-        ++col;
-
-        QPushButton *del = new QPushButton(m_innerFrame);
-        del->setObjectName(QString("%1").arg(i));
-        del->setIcon(IconLoader().load("datadelete"));
-        del->setFixedSize(WidgetScale::scaleQSize(QSize(16, 16)));
-        connect(del, SIGNAL(clicked()), this, SLOT(deleteClicked()));
-        innerLayout->addWidget(del, row, col);
-        ++col;
-
-        QLabel *dirLabel = new QLabel(m_innerFrame);
-        dirLabel->setObjectName(QString("%1").arg(i));
-        dirLabel->setText(dir);
-        innerLayout->addWidget(dirLabel, row, col);
-        innerLayout->setColumnStretch(col, 10);
-        ++col;
+    if (makeCurrent >= 0 && makeCurrent < m_path.size()) {
+        m_list->setCurrentRow(makeCurrent);
     }
 }
 
 void
 PluginPathConfigurator::upClicked()
 {
-    bool ok = false;
-    int n = sender()->objectName().toInt(&ok);
-    if (!ok) {
-        SVCERR << "upClicked: unable to find index" << endl;
-        return;
-    }
+    int current = m_list->currentRow();
+    if (current <= 0) return;
+    
     QStringList newPath;
     for (int i = 0; i < m_path.size(); ++i) {
-        if (i + 1 == n) {
+        if (i + 1 == current) {
             newPath.push_back(m_path[i+1]);
             newPath.push_back(m_path[i]);
             ++i;
@@ -118,21 +110,19 @@
         }
     }
     m_path = newPath;
-    populate();
+    
+    populate(current - 1);
 }
 
 void
 PluginPathConfigurator::downClicked()
 {
-    bool ok = false;
-    int n = sender()->objectName().toInt(&ok);
-    if (!ok) {
-        SVCERR << "downClicked: unable to find index" << endl;
-        return;
-    }
+    int current = m_list->currentRow();
+    if (current < 0 || current + 1 >= m_path.size()) return;
+    
     QStringList newPath;
     for (int i = 0; i < m_path.size(); ++i) {
-        if (i == n) {
+        if (i == current) {
             newPath.push_back(m_path[i+1]);
             newPath.push_back(m_path[i]);
             ++i;
@@ -141,24 +131,22 @@
         }
     }
     m_path = newPath;
-    populate();
+    
+    populate(current + 1);
 }
 
 void
 PluginPathConfigurator::deleteClicked()
 {
-    bool ok = false;
-    int n = sender()->objectName().toInt(&ok);
-    if (!ok) {
-        SVCERR << "deleteClicked: unable to find index" << endl;
-        return;
-    }
+    int current = m_list->currentRow();
+    
     QStringList newPath;
     for (int i = 0; i < m_path.size(); ++i) {
-        if (i != n) {
+        if (i != current) {
             newPath.push_back(m_path[i]);
         }
     }
     m_path = newPath;
-    populate();
+    
+    populate(current < m_path.size() ? current : current-1);
 }
--- a/widgets/PluginPathConfigurator.h	Tue May 15 11:18:07 2018 +0100
+++ b/widgets/PluginPathConfigurator.h	Tue May 15 13:56:26 2018 +0100
@@ -21,6 +21,7 @@
 
 class QLabel;
 class QWidget;
+class QListWidget;
 
 class PluginPathConfigurator : public QFrame
 {
@@ -43,14 +44,12 @@
     
 private:
     QGridLayout *m_layout;
+    QListWidget *m_list;
 
     QStringList m_path;
     QString m_var;
-
-    QWidget *m_innerFrame;
-    std::vector<QLabel *> m_labels;
     
-    void populate();
+    void populate(int makeCurrent = 0);
 };
 
 #endif