// Copyright (C) 2008 Lukas Lalinsky // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include #include #include #include #include #include "mainwindow.h" #include "databasetable.h" MainWindow::MainWindow() : QMainWindow(), m_model(NULL) { m_undoStack = new QUndoStack(this); setupActions(); setupUi(); newModel(); } void MainWindow::setupUi() { setWindowTitle(tr("DB Model")); resize(QSize(780, 580)); setupToolBar(); setupMenuBar(); m_properties = new TableProperties(this, 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"))); m_actionSave->setDisabled(true); 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")); m_actionSaveAs->setDisabled(true); connect(m_actionSaveAs, SIGNAL(triggered(bool)), SLOT(saveAs())); //connect(m_undoStack, SIGNAL(cleanChanged(bool)), m_actionSave, SLOT(setDisabled(bool))); //connect(m_undoStack, SIGNAL(cleanChanged(bool)), m_actionSaveAs, SLOT(setDisabled(bool))); 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); m_actionUndo = m_undoStack->createUndoAction(this, tr("&Undo")); m_actionUndo->setShortcut(QKeySequence(tr("Ctrl+Z"))); m_actionUndo->setIcon(QIcon(":/icons/16x16/edit-undo.png")); m_actionRedo = m_undoStack->createRedoAction(this, tr("Re&do")); m_actionRedo->setShortcut(QKeySequence(tr("Ctrl+Shift+Z"))); m_actionRedo->setIcon(QIcon(":/icons/16x16/edit-redo.png")); 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->addSeparator(); toolBar->addAction(m_actionUndo); toolBar->addAction(m_actionRedo); 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(m_actionUndo); menu->addAction(m_actionRedo); menu->addSeparator(); menu->addAction(tr("&Delete"), this, SLOT(deleteSelectedItems()), QKeySequence(tr("Del"))); } void MainWindow::deleteSelectedItems() { m_model->deleteSelectedItems(); } void MainWindow::open() { QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), "Database Model (*.dmf)"); 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(), "Database Model (*.dmf)"); 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); } QUndoStack * MainWindow::currentUndoStack() { return m_undoStack; }