Qt: Show Lua scripts as links in the about box.
authorGerald Combs <gerald@wireshark.org>
Wed, 31 Jan 2018 20:03:49 +0000 (12:03 -0800)
committerStig Bjørlykke <stig@bjorlykke.org>
Thu, 1 Feb 2018 18:11:22 +0000 (18:11 +0000)
Add UrlLinkDelegate::setColCheck, which lets you render strings as URLs
or plain text according to a regex. Use it to show Lua scripts as URLs
in the about box.

Open links on double clicks and add column checks.

Change-Id: Iaf5cd8a46a0b66a7d45079ba045ed2bbcb0ed005
Reviewed-on: https://code.wireshark.org/review/25542
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
ui/qt/about_dialog.cpp
ui/qt/about_dialog.h
ui/qt/models/url_link_delegate.cpp
ui/qt/models/url_link_delegate.h

index 9e9cae5ef1e017f2d7ccc60996479e32ce89d1d3..3e7725128b2b8243186e17e4a80fa768bfe3a5f7 100644 (file)
@@ -150,15 +150,8 @@ PluginListModel::PluginListModel(QObject * parent) : AStringListListModel(parent
     foreach(QStringList row, plugin_data)
     {
         QString type_name = row.at(2);
-        QString tooltip;
         typeNames_ << type_name;
-
-#ifdef HAVE_LUA
-        if (type_name == wslua_plugin_type_name()) {
-            tooltip = tr("Double-click to edit");
-        }
-#endif
-        appendRow(row, tooltip);
+        appendRow(row);
     }
 
     typeNames_.sort();
@@ -347,7 +340,7 @@ AboutDialog::AboutDialog(QWidget *parent) :
     ui->tblFolders->setTextElideMode(Qt::ElideMiddle);
     connect(ui->tblFolders, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(handleCopyMenu(QPoint)));
     connect(ui->searchFolders, SIGNAL(textChanged(QString)), folderProxyModel, SLOT(setFilter(QString)));
-    connect(ui->tblFolders, SIGNAL(clicked(QModelIndex)), this, SLOT(urlClicked(QModelIndex)));
+    connect(ui->tblFolders, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(urlDoubleClicked(QModelIndex)));
 
 
     /* Plugins */
@@ -361,6 +354,10 @@ AboutDialog::AboutDialog(QWidget *parent) :
     pluginTypeModel->setColumnToFilter(2);
     ui->tblPlugins->setModel(pluginTypeModel);
     ui->tblPlugins->setRootIsDecorated(false);
+    UrlLinkDelegate *plugin_delegate = new UrlLinkDelegate(this);
+    QString pattern = QString("^%1$").arg(wslua_plugin_type_name());
+    plugin_delegate->setColCheck(2, pattern);
+    ui->tblPlugins->setItemDelegateForColumn(3, plugin_delegate);
     ui->cmbType->addItems(pluginModel->typeNames());
     ui->tblPlugins->setContextMenuPolicy(Qt::CustomContextMenu);
     ui->tblPlugins->setTextElideMode(Qt::ElideMiddle);
@@ -442,8 +439,11 @@ void AboutDialog::showEvent(QShowEvent * event)
     QDialog::showEvent(event);
 }
 
-void AboutDialog::urlClicked(const QModelIndex &idx)
+void AboutDialog::urlDoubleClicked(const QModelIndex &idx)
 {
+    if (idx.column() != 1) {
+        return;
+    }
     QTreeView * table = qobject_cast<QTreeView *>(sender());
     if ( ! table )
         return;
@@ -553,9 +553,12 @@ void AboutDialog::copyActionTriggered(bool copyRow)
 #ifdef HAVE_LUA
 void AboutDialog::on_tblPlugins_doubleClicked(const QModelIndex &index)
 {
-    const int row = index.row();
     const int type_col = 2;
     const int path_col = 3;
+    if (index.column() != path_col) {
+        return;
+    }
+    const int row = index.row();
     const QAbstractItemModel *model = index.model();
     if (model->index(row, type_col).data().toString() == wslua_plugin_type_name()) {
         QDesktopServices::openUrl(QUrl::fromLocalFile(model->index(row, path_col).data().toString()));
index 353926bc773dc114b3c706b33e2ef4d0a5f907b2..ec1f2450cd179fb32bbfac836434fcdaf2849d0e 100644 (file)
@@ -93,7 +93,7 @@ private:
     Ui::AboutDialog *ui;
 
 private slots:
-    void urlClicked(const QModelIndex &);
+    void urlDoubleClicked(const QModelIndex &);
     void handleCopyMenu(QPoint);
     void copyActionTriggered(bool row = false);
     void copyRowActionTriggered();
index 14d05daac11fa2b360d7621144be123980814937..205a35921a3637f14e324694193d5c1707a5ab86 100644 (file)
 #include <ui/qt/models/url_link_delegate.h>
 
 #include <QPainter>
+#include <QRegExp>
 
 UrlLinkDelegate::UrlLinkDelegate(QObject *parent)
- : QStyledItemDelegate(parent)
+ : QStyledItemDelegate(parent),
+   re_col_(-1),
+   url_re_(new QRegExp())
 {}
 
+UrlLinkDelegate::~UrlLinkDelegate()
+{
+    delete url_re_;
+}
+
+void UrlLinkDelegate::setColCheck(int column, QString &pattern)
+{
+    re_col_ = column;
+    url_re_->setPattern(pattern);
+}
+
 void UrlLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
+    if (re_col_ >= 0 && url_re_) {
+        QModelIndex re_idx = index.model()->index(index.row(), re_col_);
+        QString col_text = index.model()->data(re_idx).toString();
+        if (url_re_->indexIn(col_text) < 0) {
+            QStyledItemDelegate::paint(painter, option, index);
+            return;
+        }
+    }
+
     QStyleOptionViewItem opt = option;
     initStyleOption(&opt, index);
 
index fa1a20bd6c144e8acb10c692d6064e1c27e568c3..c35a611d18cc611ad6fa1744f1e10692ca34de2b 100644 (file)
 #ifndef URL_LINK_DELEGATE_H
 #define URL_LINK_DELEGATE_H
 
-#include <config.h>
-
 #include <QStyledItemDelegate>
 #include <QStyleOptionViewItem>
 #include <QModelIndex>
-#include <QAbstractItemModel>
+
+class QRegExp;
 
 class UrlLinkDelegate : public QStyledItemDelegate
 {
@@ -24,9 +23,17 @@ class UrlLinkDelegate : public QStyledItemDelegate
 
 public:
     explicit UrlLinkDelegate(QObject *parent = Q_NULLPTR);
+    ~UrlLinkDelegate();
+    // If pattern matches the string in column, render as a URL.
+    // Otherwise render as plain text.
+    void setColCheck(int column, QString &pattern);
 
 protected:
     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+
+private:
+    int re_col_;
+    QRegExp *url_re_;
 };
 #endif // URL_LINK_DELEGATE_H