Skip to content

Commit 61c8f47

Browse files
committed
Open URL or filenames from DB cells
A new button is added in the "Edit DB Cell" dock for interpreting the current value as a URL or filename and opening it using the default application for the file or a web browser for the URL. The same can be directly invoked in the table browser using the same shortcut as the FK navigation. Related issue #1597
1 parent 8b252a9 commit 61c8f47

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/EditDialog.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QPainter>
2020
#include <QClipboard>
2121
#include <QTextDocument>
22+
#include <QDesktopServices>
2223

2324
#include <Qsci/qsciscintilla.h>
2425
#include <json.hpp>
@@ -65,6 +66,25 @@ EditDialog::EditDialog(QWidget* parent)
6566
ui->editorBinary->addAction(ui->actionPrint);
6667
ui->editorBinary->addAction(ui->actionCopyHexAscii);
6768

69+
connect(ui->actionOpenInApp, &QAction::triggered, this, [&]() {
70+
QUrl url;
71+
switch (dataSource) {
72+
case SciBuffer:
73+
url.setUrl(sciEdit->text(), QUrl::TolerantMode);
74+
break;
75+
case QtBuffer:
76+
url.setUrl(ui->qtEdit->toPlainText(), QUrl::TolerantMode);;
77+
break;
78+
default:
79+
return;
80+
}
81+
if(url.isValid())
82+
QDesktopServices::openUrl(url);
83+
else
84+
QMessageBox::warning(this, QApplication::applicationName(),
85+
tr("Content is not a valid URL or filename: %1").arg(url.errorString()));
86+
});
87+
6888
mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool();
6989
ui->actionIndent->setChecked(mustIndentAndCompact);
7090

src/EditDialog.ui

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<addaction name="actionIndent"/>
125125
<addaction name="actionImport"/>
126126
<addaction name="actionExport"/>
127+
<addaction name="actionOpenInApp"/>
127128
<addaction name="actionNull"/>
128129
<addaction name="actionPrint"/>
129130
</widget>
@@ -175,8 +176,8 @@ Errors are indicated with a red squiggle underline.</string>
175176
<rect>
176177
<x>0</x>
177178
<y>0</y>
178-
<width>598</width>
179-
<height>264</height>
179+
<width>84</width>
180+
<height>35</height>
180181
</rect>
181182
</property>
182183
<layout class="QVBoxLayout" name="verticalLayout_2">
@@ -402,6 +403,24 @@ Errors are indicated with a red squiggle underline.</string>
402403
<string>Wrap lines on word boundaries</string>
403404
</property>
404405
</action>
406+
<action name="actionOpenInApp">
407+
<property name="icon">
408+
<iconset resource="icons/icons.qrc">
409+
<normaloff>:/icons/open_in_app</normaloff>:/icons/open_in_app</iconset>
410+
</property>
411+
<property name="text">
412+
<string>Open in default application or browser</string>
413+
</property>
414+
<property name="iconText">
415+
<string>Open in application</string>
416+
</property>
417+
<property name="toolTip">
418+
<string>Open in default application or browser</string>
419+
</property>
420+
<property name="whatsThis">
421+
<string>The value is interpreted as a file or URL and opened in the default application or web browser.</string>
422+
</property>
423+
</action>
405424
</widget>
406425
<tabstops>
407426
<tabstop>comboMode</tabstop>

src/ExtendedTableWidget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QCompleter>
2525
#include <QComboBox>
2626
#include <QShortcut>
27+
#include <QDesktopServices>
2728

2829
#include <limits>
2930

@@ -901,6 +902,17 @@ void ExtendedTableWidget::cellClicked(const QModelIndex& index)
901902
emit foreignKeyClicked(sqlb::ObjectIdentifier(m->currentTableName().schema(), fk.table()),
902903
fk.columns().size() ? fk.columns().at(0) : "",
903904
m->data(index, Qt::EditRole).toByteArray());
905+
else {
906+
// If this column does not have a foreign-key, try to interpret it as a filename/URL and open it in external application.
907+
908+
// TODO: Qt is doing a contiguous selection when Control+Click is pressed. It should be disabled, but at least moving the
909+
// current index gives better result.
910+
setCurrentIndex(index);
911+
912+
QUrl url (model()->data(index, Qt::EditRole).toString(), QUrl::TolerantMode);
913+
if(url.isValid())
914+
QDesktopServices::openUrl(url);
915+
}
904916
}
905917
}
906918

src/icons/application_link.png

701 Bytes
Loading

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@
9898
<file alias="edit_cond_formats">style_edit.png</file>
9999
<file alias="clear_cond_formats">style_delete.png</file>
100100
<file alias="add_cond_format">style_add.png</file>
101+
<file alias="open_in_app">application_link.png</file>
101102
</qresource>
102103
</RCC>

0 commit comments

Comments
 (0)