Skip to content

Commit

Permalink
File Extension management interface (sqlitebrowser#659)
Browse files Browse the repository at this point in the history
[NEW] Add new interface and functionality to manage database file
extension. (Implements feature request sqlitebrowser#659)
  • Loading branch information
dmatorino authored and MKleusberg committed Jan 13, 2018
1 parent b08960f commit a993c19
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ set(SQLB_MOC_HDR
src/RemotePushDialog.h
src/FindReplaceDialog.h
src/ExtendedScintilla.h
src/FileExtensionManager.h
)

set(SQLB_SRC
Expand Down Expand Up @@ -164,6 +165,7 @@ set(SQLB_SRC
src/RemotePushDialog.cpp
src/FindReplaceDialog.cpp
src/ExtendedScintilla.cpp
src/FileExtensionManager.cpp
src/Data.cpp
)

Expand All @@ -185,6 +187,7 @@ set(SQLB_FORMS
src/RemoteDock.ui
src/RemotePushDialog.ui
src/FindReplaceDialog.ui
src/FileExtensionManager.ui
)

set(SQLB_RESOURCES
Expand Down
5 changes: 5 additions & 0 deletions src/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ void FileDialog::setFileDialogPath(const QString& new_path)
break; // Do nothing
}
}

QString FileDialog::getSqlDatabaseFileFilter()
{
return Settings::getValue("General", "DBFileExtensions").toString() + ";;" + QObject::tr("All files (*)"); //Always add "All files (*)" to the available filters
}
5 changes: 1 addition & 4 deletions src/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class FileDialog : public QFileDialog
static QString getExistingDirectory(QWidget* parent = nullptr, const QString& caption = QString(),
Options options = 0);

static QString getSqlDatabaseFileFilter()
{
return QObject::tr("SQLite database files (*.db *.sqlite *.sqlite3 *.db3);;All files (*)");
}
static QString getSqlDatabaseFileFilter();

private:
static QString getFileDialogPath();
Expand Down
113 changes: 113 additions & 0 deletions src/FileExtensionManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "FileExtensionManager.h"
#include "ui_FileExtensionManager.h"

FileExtensionManager::FileExtensionManager(QStringList init, QWidget *parent) :
QDialog(parent),
ui(new Ui::FileExtensionManager)
{
ui->setupUi(this);

int i = 0;
foreach(QString itemString, init)
{
QString description = itemString.left(itemString.indexOf('(')).trimmed();
QString extension = itemString;
extension = extension.remove (0, itemString.indexOf('(')+1).remove(')').simplified().trimmed();
if ( extension.compare("*") != 0 ) //We exclude "All files" from the table
{
QTableWidgetItem *newItemDescription = new QTableWidgetItem(description);
QTableWidgetItem *newItemExtension = new QTableWidgetItem(extension);
ui->tableExtensions->insertRow(i);
ui->tableExtensions->setItem(i, 0, newItemDescription);
ui->tableExtensions->setItem(i, 1, newItemExtension);
i++;
}
}

connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addItem()));
connect(ui->buttonRemove, SIGNAL(clicked(bool)), this, SLOT(removeItem()));

connect(ui->buttonDown, SIGNAL(clicked(bool)), this, SLOT(downItem()));
connect(ui->buttonUp, SIGNAL(clicked(bool)), this, SLOT(upItem()));
}

FileExtensionManager::~FileExtensionManager()
{
delete ui;
}

void FileExtensionManager::addItem()
{
int i = ui->tableExtensions->rowCount();
ui->tableExtensions->insertRow(i);
QTableWidgetItem *newItemDescription = new QTableWidgetItem(tr("Description"));
QTableWidgetItem *newItemExtension = new QTableWidgetItem(tr("*.extension"));
ui->tableExtensions->setItem(i, 0, newItemDescription);
ui->tableExtensions->setItem(i, 1, newItemExtension);
}

void FileExtensionManager::removeItem()
{
QList<int> selectedRows;
foreach (QTableWidgetItem *item, ui->tableExtensions->selectedItems())
{
if (selectedRows.contains(item->row()) == false)
{
selectedRows.append(item->row());
}
}

qSort(selectedRows);

for (int i = selectedRows.size()-1; i >= 0; --i)
{
ui->tableExtensions->removeRow(selectedRows[i]);
}
}

void FileExtensionManager::upItem()
{
if (ui->tableExtensions->selectedItems().isEmpty()) return;

int selectedRow = ui->tableExtensions->selectedItems().first()->row();
if(selectedRow == 0)
return;

QTableWidgetItem *t1, *t2;
t1 = ui->tableExtensions->takeItem(selectedRow, 0);
t2 = ui->tableExtensions->takeItem(selectedRow, 1);
ui->tableExtensions->removeRow(selectedRow);
ui->tableExtensions->insertRow(selectedRow-1);
ui->tableExtensions->setItem(selectedRow-1, 0, t1);
ui->tableExtensions->setItem(selectedRow-1, 1, t2);
ui->tableExtensions->selectRow(selectedRow-1);
}

void FileExtensionManager::downItem()
{
if (ui->tableExtensions->selectedItems().isEmpty()) return;

int selectedRow = ui->tableExtensions->selectedItems().first()->row();
if(selectedRow == ui->tableExtensions->rowCount() - 1)
return;

QTableWidgetItem *t1, *t2;
t1 = ui->tableExtensions->takeItem(selectedRow, 0);
t2 = ui->tableExtensions->takeItem(selectedRow, 1);
ui->tableExtensions->removeRow(selectedRow);
ui->tableExtensions->insertRow(selectedRow+1);
ui->tableExtensions->setItem(selectedRow+1, 0, t1);
ui->tableExtensions->setItem(selectedRow+1, 1, t2);
ui->tableExtensions->selectRow(selectedRow+1);
}

QStringList FileExtensionManager::getDBFileExtensions()
{
QStringList result;
for (int i = 0; i < ui->tableExtensions->rowCount(); ++i)
{
result.append(QString("%1 (%2)").arg(ui->tableExtensions->item(i, 0)->text()).arg(ui->tableExtensions->item(i, 1)->text()));
}
return result;
}

30 changes: 30 additions & 0 deletions src/FileExtensionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef FILEEXTENSIONMANAGER_H
#define FILEEXTENSIONMANAGER_H

#include <QDialog>

namespace Ui {
class FileExtensionManager;
}

class FileExtensionManager : public QDialog
{
Q_OBJECT

public:
explicit FileExtensionManager(QStringList init, QWidget *parent = nullptr);
~FileExtensionManager();

QStringList getDBFileExtensions();

private:
Ui::FileExtensionManager *ui;

public slots:
void addItem();
void removeItem();
void upItem();
void downItem();
};

#endif // FILEEXTENSIONMANAGER_H
153 changes: 153 additions & 0 deletions src/FileExtensionManager.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>FileExtensionManager</class>
<widget class="QDialog" name="FileExtensionManager">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>578</width>
<height>463</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="buttonUp">
<property name="text">
<string>&amp;Up</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/up</normaloff>:/icons/up</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDown">
<property name="text">
<string>&amp;Down</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/down</normaloff>:/icons/down</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonAdd">
<property name="text">
<string>&amp;Add</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/field_add</normaloff>:/icons/field_add</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonRemove">
<property name="text">
<string>&amp;Remove</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/field_delete</normaloff>:/icons/field_delete</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTableWidget" name="tableExtensions">
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>100</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>100</number>
</attribute>
<column>
<property name="text">
<string>Description</string>
</property>
</column>
<column>
<property name="text">
<string>Extensions</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="icons/icons.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>FileExtensionManager</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>FileExtensionManager</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
16 changes: 15 additions & 1 deletion src/PreferencesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Application.h"
#include "MainWindow.h"
#include "RemoteDatabase.h"
#include "FileExtensionManager.h"

#include <QDir>
#include <QColorDialog>
Expand All @@ -14,7 +15,8 @@

PreferencesDialog::PreferencesDialog(QWidget* parent)
: QDialog(parent),
ui(new Ui::PreferencesDialog)
ui(new Ui::PreferencesDialog),
m_dbFileExtensions(FileDialog::getSqlDatabaseFileFilter().split(";;"))
{
ui->setupUi(this);
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
Expand Down Expand Up @@ -280,6 +282,8 @@ void PreferencesDialog::saveSettings()
Settings::setValue("General", "language", newLanguage);
Settings::setValue("General", "toolbarStyle", ui->toolbarStyleComboBox->currentIndex());

Settings::setValue("General", "DBFileExtensions", m_dbFileExtensions.join(";;") );

accept();
}

Expand Down Expand Up @@ -564,3 +568,13 @@ void PreferencesDialog::updatePreviewFont()
ui->txtBlob->setFont(textFont);
}
}

void PreferencesDialog::on_buttonManageFileExtension_clicked()
{
FileExtensionManager *manager = new FileExtensionManager(m_dbFileExtensions, this);

if(manager->exec() == QDialog::Accepted)
{
m_dbFileExtensions = manager->getDBFileExtensions();
}
}
4 changes: 4 additions & 0 deletions src/PreferencesDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ private slots:
void chooseRemoteCloneDirectory();
void updatePreviewFont();

void on_buttonManageFileExtension_clicked();

private:
Ui::PreferencesDialog *ui;

QStringList m_dbFileExtensions;

void fillLanguageBox();
void loadColorSetting(QFrame *frame, const QString &name);
void setColorSetting(QFrame *frame, const QColor &color);
Expand Down
Loading

0 comments on commit a993c19

Please sign in to comment.