summaryrefslogtreecommitdiff
path: root/mainwindow.cpp
diff options
context:
space:
mode:
authorLukáš Lalinský <lalinsky@gmail.com>2008-11-23 22:44:06 +0100
committerLukáš Lalinský <lalinsky@gmail.com>2008-11-23 22:44:06 +0100
commit5da39fc03fe42f7e4b572374e983ea1afb80033d (patch)
tree4d6fcca41e63b37f8c77577c55d89b510fe959fe /mainwindow.cpp
downloaddbmodel-5da39fc03fe42f7e4b572374e983ea1afb80033d.tar.xz
Initial commit
Diffstat (limited to 'mainwindow.cpp')
-rw-r--r--mainwindow.cpp230
1 files changed, 230 insertions, 0 deletions
diff --git a/mainwindow.cpp b/mainwindow.cpp
new file mode 100644
index 0000000..9d090f8
--- /dev/null
+++ b/mainwindow.cpp
@@ -0,0 +1,230 @@
+#include <QMenuBar>
+#include <QMenu>
+#include <QComboBox>
+#include <QToolBar>
+#include <QFileDialog>
+#include <QIcon>
+#include <QDebug>
+#include "mainwindow.h"
+#include "databasetable.h"
+
+MainWindow::MainWindow()
+ : QMainWindow(), m_model(NULL)
+{
+ setupActions();
+ setupUi();
+ newModel();
+}
+
+void
+MainWindow::setupUi()
+{
+ setWindowTitle(tr("DB Model"));
+ resize(QSize(780, 580));
+
+ setupToolBar();
+ setupMenuBar();
+
+ m_properties = new TableProperties(this);
+
+ m_view = new DatabaseModelView(this);
+
+ m_splitter = new QSplitter(Qt::Vertical, this);
+ setCentralWidget(m_splitter);
+ m_splitter->addWidget(m_view);
+ m_splitter->addWidget(m_properties);
+
+ m_splitter->setStretchFactor(0, 6);
+ m_splitter->setStretchFactor(1, 1);
+}
+
+void
+MainWindow::setupActions()
+{
+ m_actionNew = new QAction(this);
+ m_actionNew->setText(tr("&New"));
+ m_actionNew->setIcon(QIcon(":/icons/16x16/document-new.png"));
+ m_actionNew->setShortcut(QKeySequence(tr("Ctrl+N")));
+ connect(m_actionNew, SIGNAL(triggered(bool)), SLOT(newModel()));
+
+ m_actionOpen = new QAction(this);
+ m_actionOpen->setText(tr("&Open..."));
+ m_actionOpen->setIcon(QIcon(":/icons/16x16/document-open.png"));
+ m_actionOpen->setShortcut(QKeySequence(tr("Ctrl+O")));
+ connect(m_actionOpen, SIGNAL(triggered(bool)), SLOT(open()));
+
+ m_actionSave = new QAction(this);
+ m_actionSave->setText(tr("&Save"));
+ m_actionSave->setIcon(QIcon(":/icons/16x16/document-save.png"));
+ m_actionSave->setShortcut(QKeySequence(tr("Ctrl+S")));
+ connect(m_actionSave, SIGNAL(triggered(bool)), SLOT(save()));
+
+ m_actionSaveAs = new QAction(this);
+ m_actionSaveAs->setText(tr("Save &As..."));
+ m_actionSaveAs->setIcon(QIcon(":/icons/16x16/document-save-as.png"));
+ connect(m_actionSaveAs, SIGNAL(triggered(bool)), SLOT(saveAs()));
+
+ m_actionSwitchMode[0] = new QAction(this);
+ m_actionSwitchMode[0]->setText(tr("Select"));
+ m_actionSwitchMode[0]->setIcon(QIcon(":/icons/cr16-action-mouse_pointer.png"));
+ m_actionSwitchMode[0]->setCheckable(true);
+ m_actionSwitchMode[0]->setChecked(true);
+
+ m_actionSwitchMode[1] = new QAction(this);
+ m_actionSwitchMode[1]->setText(tr("Add new table"));
+ m_actionSwitchMode[1]->setIcon(QIcon(":/icons/cr16-action-table_newobj.png"));
+ m_actionSwitchMode[1]->setCheckable(true);
+
+ m_actionSwitchMode[2] = new QAction(this);
+ m_actionSwitchMode[2]->setText(tr("Add new relation"));
+ m_actionSwitchMode[2]->setIcon(QIcon(":/icons/cr16-action-relation_newobj.png"));
+ m_actionSwitchMode[2]->setCheckable(true);
+
+ connect(m_actionSwitchMode[0], SIGNAL(triggered(bool)), SLOT(switchModeSelect()));
+ connect(m_actionSwitchMode[1], SIGNAL(triggered(bool)), SLOT(switchModeAddTable()));
+ connect(m_actionSwitchMode[2], SIGNAL(triggered(bool)), SLOT(switchModeAddRelation()));
+}
+
+void
+MainWindow::setupToolBar()
+{
+ QToolBar *toolBar = addToolBar(tr("&File"));
+ toolBar->setIconSize(QSize(16, 16));
+ toolBar->addAction(m_actionNew);
+ toolBar->addAction(m_actionOpen);
+ toolBar->addSeparator();
+ toolBar->addAction(m_actionSave);
+ toolBar->addAction(m_actionSaveAs);
+ toolBar = addToolBar(tr("&Mode"));
+ toolBar->setIconSize(QSize(16, 16));
+ toolBar->addAction(m_actionSwitchMode[0]);
+ toolBar->addAction(m_actionSwitchMode[1]);
+ toolBar->addAction(m_actionSwitchMode[2]);
+
+ QComboBox *sceneScaleCombo = new QComboBox;
+ QStringList scales;
+ scales << tr("50%") << tr("70%") << tr("85%") << tr("100%") << tr("125%") << tr("150%");
+ sceneScaleCombo->addItems(scales);
+ sceneScaleCombo->setEditable(true);
+ sceneScaleCombo->setCurrentIndex(3);
+ connect(sceneScaleCombo, SIGNAL(currentIndexChanged(const QString &)),
+ this, SLOT(setViewScale(const QString &)));
+
+ toolBar = addToolBar(tr("View"));
+ toolBar->addWidget(sceneScaleCombo);
+
+}
+
+void
+MainWindow::setViewScale(const QString &scale)
+{
+ double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
+ QMatrix oldMatrix = m_view->matrix();
+ m_view->resetMatrix();
+ m_view->translate(oldMatrix.dx(), oldMatrix.dy());
+ m_view->scale(newScale, newScale);
+}
+
+void
+MainWindow::setupMenuBar()
+{
+ QMenu *menu = menuBar()->addMenu("&File");
+ menu->addAction(m_actionNew);
+ menu->addAction(m_actionOpen);
+ menu->addSeparator();
+ menu->addAction(m_actionSave);
+ menu->addAction(m_actionSaveAs);
+ menu->addSeparator();
+ menu->addAction(tr("&Quit"), this, SLOT(close()), QKeySequence(tr("Ctrl+Q")));
+
+ menu = menuBar()->addMenu("&Edit");
+ menu->addAction(tr("&Delete"), this, SLOT(deleteSelectedItems()), QKeySequence(tr("Del")));
+}
+
+void
+MainWindow::deleteSelectedItems()
+{
+ m_model->deleteSelectedItems();
+}
+
+void
+MainWindow::open()
+{
+ QString fileName = QFileDialog::getOpenFileName(this);
+ if (!fileName.isNull()) {
+ DatabaseModel *model = new DatabaseModel(this);
+ model->load(fileName);
+ newModel(model);
+ }
+}
+
+void
+MainWindow::save()
+{
+ QString fileName = m_model->fileName();
+ if (fileName.isEmpty()) {
+ saveAs();
+ }
+ else {
+ m_model->save(fileName);
+ }
+}
+
+void
+MainWindow::saveAs()
+{
+ QString fileName = QFileDialog::getSaveFileName(this, QString(), m_model->fileName());
+ if (!fileName.isNull()) {
+ m_model->save(fileName);
+ }
+}
+
+void
+MainWindow::newModel(DatabaseModel *newModel)
+{
+ if (!newModel)
+ newModel = new DatabaseModel(this);
+
+ m_view->setScene(newModel);
+ if (m_model)
+ delete m_model;
+ m_model = newModel;
+
+ connect(m_model,
+ SIGNAL(modeChanged(DatabaseModel::Mode)),
+ SLOT(updateMode(DatabaseModel::Mode)));
+ connect(m_model, SIGNAL(selectionChanged()), SLOT(updateSelection()));
+}
+
+void
+MainWindow::updateSelection()
+{
+ DatabaseTable *table = m_model->selectedTable();
+ m_properties->setTable(table);
+}
+
+void
+MainWindow::updateMode(DatabaseModel::Mode mode)
+{
+ m_actionSwitchMode[0]->setChecked(mode == DatabaseModel::Select);
+ m_actionSwitchMode[1]->setChecked(mode == DatabaseModel::AddTable);
+ m_actionSwitchMode[2]->setChecked(mode == DatabaseModel::AddRelation);
+}
+
+void
+MainWindow::switchModeSelect()
+{
+ m_model->setMode(DatabaseModel::Select);
+}
+
+void
+MainWindow::switchModeAddTable()
+{
+ m_model->setMode(DatabaseModel::AddTable);
+}
+
+void
+MainWindow::switchModeAddRelation()
+{
+ m_model->setMode(DatabaseModel::AddRelation);
+}