Skip to content

Commit 4041f4a

Browse files
author
Jim Hamill
committed
About to break stuff
1 parent 901c2cb commit 4041f4a

13 files changed

+337
-34
lines changed

ExplorerWidget.cpp

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QSqlQuery>
77
#include <QSqlError>
88
#include <QInputDialog>
9+
#include <QMenu>
910

1011
ExplorerWidget::ExplorerWidget(QWidget *parent) :
1112
QWidget(parent),
@@ -14,6 +15,15 @@ ExplorerWidget::ExplorerWidget(QWidget *parent) :
1415
m_schemaWidget(NULL)
1516
{
1617
ui->setupUi(this);
18+
19+
// Connect actions
20+
connect(ui->actionAdd_Table, SIGNAL(triggered()), this, SLOT(addTable()));
21+
connect(ui->actionRemove_Table, SIGNAL(triggered()), this, SLOT(removeTable()));
22+
connect(ui->actionRename_Table, SIGNAL(triggered()), this, SLOT(renameTable()));
23+
24+
// Connect buttons
25+
connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addTable()));
26+
connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(removeTable()));
1727
}
1828

1929
ExplorerWidget::~ExplorerWidget()
@@ -29,6 +39,16 @@ void ExplorerWidget::init(QSqlDatabase *database, Extension *extension)
2939
// Set the extension
3040
m_extension = extension;
3141

42+
// Hide add button if it's not supported
43+
if (!m_extension || !m_extension->hasCapability(ADD_TABLE)) {
44+
ui->addButton->setVisible(false);
45+
}
46+
47+
// Hide remove button if it's not supported
48+
if (!m_extension || !m_extension->hasCapability(REMOVE_TABLE)) {
49+
ui->removeButton->setVisible(false);
50+
}
51+
3252
// Set the database for child widgets
3353
ui->browseTab->setDatabase(m_database);
3454

@@ -69,42 +89,69 @@ void ExplorerWidget::on_tableListWidget_itemActivated(QListWidgetItem *item)
6989
m_schemaWidget->setTable(item->text());
7090
}
7191

72-
void ExplorerWidget::on_removeButton_clicked()
92+
void ExplorerWidget::addTable()
93+
{
94+
// Get table name
95+
QString table = QInputDialog::getText(this, "Create Table", "Enter table name");
96+
97+
// Create table if extension supports it
98+
m_extension->createTable(table);
99+
100+
// Refresh explorer
101+
refresh();
102+
}
103+
104+
void ExplorerWidget::removeTable()
73105
{
74106
// Display messagebox for confirmation
75107
if (QMessageBox::Yes == QMessageBox::question(this, "Drop Table", "Are you sure you want to drop this table?")) {
76108

77109
// Get table name
78110
QString table = ui->tableListWidget->currentItem()->text();
79111

80-
// Drop table if extension supports it
81-
if (m_extension) {
82-
m_extension->removeTable(table);
83-
}
84-
85-
else {
86-
QMessageBox::warning(this, "Sorry", "This functionality is not available for this database type yet");
87-
}
112+
// Drop table
113+
m_extension->removeTable(table);
88114

89115
// Refresh explorer
90116
refresh();
91117
}
92118
}
93119

94-
void ExplorerWidget::on_addButton_clicked()
120+
void ExplorerWidget::renameTable()
95121
{
96-
// Get table name
97-
QString table = QInputDialog::getText(this, "Create Table", "Enter table name");
122+
// Get old table name
123+
QString from = ui->tableListWidget->currentItem()->text();
98124

99-
// Drop table if extension supports it
100-
if (m_extension) {
101-
m_extension->createTable(table);
102-
}
125+
// Get new table name
126+
QString to = QInputDialog::getText(this, "Rename table", "Enter new table name");
103127

104-
else {
105-
QMessageBox::warning(this, "Sorry", "This functionality is not available for this database type yet");
106-
}
128+
// Rename table
129+
m_extension->renameTable(from, to);
107130

108131
// Refresh explorer
109132
refresh();
110133
}
134+
135+
void ExplorerWidget::on_tableListWidget_customContextMenuRequested(const QPoint &pos)
136+
{
137+
// Setup context menu
138+
QMenu *menu = new QMenu(this);
139+
140+
// If our extension can add tables
141+
if (m_extension && m_extension->hasCapability(ADD_TABLE)) {
142+
menu->addAction(ui->actionAdd_Table);
143+
}
144+
145+
// If our extension can remove tables
146+
if (m_extension && m_extension->hasCapability(REMOVE_TABLE)) {
147+
menu->addAction(ui->actionRemove_Table);
148+
}
149+
150+
// If our extension can rename tables
151+
if (m_extension && m_extension->hasCapability(RENAME_TABLE)) {
152+
menu->addAction(ui->actionRename_Table);
153+
}
154+
155+
// Show the menu
156+
menu->popup(ui->tableListWidget->viewport()->mapToGlobal(pos));
157+
}

ExplorerWidget.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class ExplorerWidget : public QWidget
2222
void init(QSqlDatabase *database, Extension *extension = NULL);
2323

2424
public slots:
25+
void addTable();
26+
void removeTable();
27+
void renameTable();
2528
void refresh();
2629

2730
private slots:
2831
void on_tableListWidget_itemActivated(QListWidgetItem *item);
29-
30-
void on_removeButton_clicked();
31-
32-
void on_addButton_clicked();
32+
void on_tableListWidget_customContextMenuRequested(const QPoint &pos);
3333

3434
private:
3535
Ui::ExplorerWidget *ui;

ExplorerWidget.ui

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<height>16777215</height>
3535
</size>
3636
</property>
37+
<property name="contextMenuPolicy">
38+
<enum>Qt::CustomContextMenu</enum>
39+
</property>
3740
</widget>
3841
</item>
3942
<item>
@@ -81,6 +84,30 @@
8184
</widget>
8285
</item>
8386
</layout>
87+
<action name="actionAdd_Table">
88+
<property name="icon">
89+
<iconset theme="list-add"/>
90+
</property>
91+
<property name="text">
92+
<string>Add Table</string>
93+
</property>
94+
</action>
95+
<action name="actionRemove_Table">
96+
<property name="icon">
97+
<iconset theme="list-remove"/>
98+
</property>
99+
<property name="text">
100+
<string>Remove Table</string>
101+
</property>
102+
</action>
103+
<action name="actionRename_Table">
104+
<property name="icon">
105+
<iconset theme="document-edit"/>
106+
</property>
107+
<property name="text">
108+
<string>Rename Table</string>
109+
</property>
110+
</action>
84111
</widget>
85112
<customwidgets>
86113
<customwidget>

Extension/Extension.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ Extension::~Extension()
3939

4040
}
4141

42+
int Extension::hasCapability(Capability capability)
43+
{
44+
return false;
45+
}
46+
47+
int Extension::importDatabase()
48+
{
49+
return false;
50+
}
51+
52+
int Extension::exportDatabase()
53+
{
54+
return false;
55+
}
56+
57+
int Extension::clearDatabase()
58+
{
59+
return false;
60+
}
61+
4262
int Extension::createTable(QString table)
4363
{
4464
return false;
@@ -49,6 +69,21 @@ int Extension::removeTable(QString table)
4969
return false;
5070
}
5171

72+
int Extension::renameTable(QString from, QString to)
73+
{
74+
return false;
75+
}
76+
77+
int Extension::addColumn(QString table)
78+
{
79+
return false;
80+
}
81+
82+
int Extension::removeColumn(QString table, QString column)
83+
{
84+
return false;
85+
}
86+
5287
void Extension::addRelations(QSqlRelationalTableModel *model)
5388
{
5489

Extension/Extension.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ class ExtensionTab
2626
QWidget *m_widget;
2727
};
2828

29+
enum Capability
30+
{
31+
IMPORT_DATABASE = 1,
32+
EXPORT_DATABASE = 2,
33+
CLEAR_DATABASE = 3,
34+
35+
ADD_TABLE = 11,
36+
REMOVE_TABLE = 12,
37+
RENAME_TABLE = 13,
38+
39+
ADD_COLUMN = 21,
40+
REMOVE_COLUMN = 22
41+
};
42+
2943
class Extension : public QObject
3044
{
3145
Q_OBJECT
@@ -34,8 +48,22 @@ Q_OBJECT
3448
explicit Extension(QObject *parent = 0, QSqlDatabase *database = new QSqlDatabase());
3549
~Extension();
3650

51+
// Defines capabilities
52+
virtual int hasCapability(Capability capability);
53+
54+
// Database functions
55+
virtual int importDatabase();
56+
virtual int exportDatabase();
57+
virtual int clearDatabase();
58+
59+
// Table functions
3760
virtual int createTable(QString table);
3861
virtual int removeTable(QString table);
62+
virtual int renameTable(QString from, QString to);
63+
64+
// Schema functions
65+
virtual int addColumn(QString table);
66+
virtual int removeColumn(QString table, QString column);
3967

4068
virtual void addRelations(QSqlRelationalTableModel *model);
4169

0 commit comments

Comments
 (0)