annotate widgets/ModelDataTableDialog.cpp @ 400:32acd578fcba

* Basic implementation of add and remove point in data editor * Improve resilience of frame - real-time - frame round-trip conversions
author Chris Cannam
date Mon, 16 Jun 2008 14:48:42 +0000
parents 80c7dd3c8dce
children 96531861b2f3
rev   line source
Chris@392 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@392 2
Chris@392 3 /*
Chris@392 4 Sonic Visualiser
Chris@392 5 An audio file viewer and annotation editor.
Chris@392 6 Centre for Digital Music, Queen Mary, University of London.
Chris@392 7 This file copyright 2008 QMUL.
Chris@392 8
Chris@392 9 This program is free software; you can redistribute it and/or
Chris@392 10 modify it under the terms of the GNU General Public License as
Chris@392 11 published by the Free Software Foundation; either version 2 of the
Chris@392 12 License, or (at your option) any later version. See the file
Chris@392 13 COPYING included with this distribution for more information.
Chris@392 14 */
Chris@392 15
Chris@392 16 #include "ModelDataTableDialog.h"
Chris@392 17
Chris@392 18 #include "data/model/ModelDataTableModel.h"
Chris@395 19 #include "data/model/TabularModel.h"
Chris@398 20 #include "data/model/Model.h"
Chris@392 21
Chris@393 22 #include "CommandHistory.h"
Chris@399 23 #include "IconLoader.h"
Chris@393 24
Chris@392 25 #include <QTableView>
Chris@392 26 #include <QGridLayout>
Chris@392 27 #include <QGroupBox>
Chris@392 28 #include <QDialogButtonBox>
Chris@392 29 #include <QHeaderView>
Chris@392 30 #include <QApplication>
Chris@392 31 #include <QDesktopWidget>
Chris@399 32 #include <QAction>
Chris@399 33 #include <QToolBar>
Chris@392 34
Chris@393 35 #include <iostream>
Chris@393 36
Chris@398 37 ModelDataTableDialog::ModelDataTableDialog(TabularModel *model, QString title, QWidget *parent) :
Chris@400 38 QMainWindow(parent),
Chris@400 39 m_currentRow(0)
Chris@392 40 {
Chris@392 41 setWindowTitle(tr("Data Editor"));
Chris@392 42
Chris@396 43 QToolBar *toolbar = addToolBar(tr("Toolbar"));
Chris@399 44
Chris@399 45 IconLoader il;
Chris@399 46
Chris@399 47 QAction *action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
Chris@399 48 action->setShortcut(tr("Insert"));
Chris@399 49 action->setStatusTip(tr("Insert a new item"));
Chris@399 50 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
Chris@399 51 toolbar->addAction(action);
Chris@399 52
Chris@399 53 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
Chris@399 54 action->setShortcut(tr("Delete"));
Chris@399 55 action->setStatusTip(tr("Delete the selected item or items"));
Chris@400 56 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
Chris@399 57 toolbar->addAction(action);
Chris@399 58
Chris@399 59 action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
Chris@399 60 action->setShortcut(tr("Edit"));
Chris@399 61 action->setStatusTip(tr("Edit the selected item"));
Chris@399 62 connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
Chris@399 63 toolbar->addAction(action);
Chris@399 64
Chris@396 65 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@396 66
Chris@396 67 QFrame *mainFrame = new QFrame;
Chris@396 68 setCentralWidget(mainFrame);
Chris@396 69
Chris@392 70 QGridLayout *grid = new QGridLayout;
Chris@396 71 mainFrame->setLayout(grid);
Chris@392 72
Chris@392 73 QGroupBox *box = new QGroupBox;
Chris@398 74 if (title != "") {
Chris@398 75 box->setTitle(title);
Chris@398 76 } else {
Chris@398 77 box->setTitle(tr("Data in Layer"));
Chris@398 78 }
Chris@392 79 grid->addWidget(box, 0, 0);
Chris@392 80 grid->setRowStretch(0, 15);
Chris@392 81
Chris@392 82 QGridLayout *subgrid = new QGridLayout;
Chris@392 83 box->setLayout(subgrid);
Chris@392 84
Chris@392 85 subgrid->setSpacing(0);
Chris@392 86 subgrid->setMargin(5);
Chris@392 87
Chris@392 88 m_tableView = new QTableView;
Chris@392 89 subgrid->addWidget(m_tableView);
Chris@392 90
Chris@399 91 // m_tableView->verticalHeader()->hide();
Chris@392 92 // m_tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
Chris@395 93 m_tableView->setSortingEnabled(true);
Chris@396 94 m_tableView->sortByColumn(0, Qt::AscendingOrder);
Chris@392 95
Chris@392 96 m_table = new ModelDataTableModel(model);
Chris@392 97 m_tableView->setModel(m_table);
Chris@392 98
Chris@393 99 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
Chris@393 100 this, SLOT(viewClicked(const QModelIndex &)));
Chris@393 101 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
Chris@393 102 this, SLOT(viewPressed(const QModelIndex &)));
Chris@400 103 connect(m_tableView->selectionModel(),
Chris@400 104 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
Chris@400 105 this,
Chris@400 106 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
Chris@400 107 connect(m_table, SIGNAL(addCommand(Command *)),
Chris@400 108 this, SLOT(addCommand(Command *)));
Chris@393 109
Chris@392 110 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@396 111 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@392 112 grid->addWidget(bb, 2, 0);
Chris@392 113 grid->setRowStretch(2, 0);
Chris@392 114
Chris@392 115 QDesktopWidget *desktop = QApplication::desktop();
Chris@392 116 QRect available = desktop->availableGeometry();
Chris@392 117
Chris@392 118 int width = available.width() / 3;
Chris@392 119 int height = available.height() / 2;
Chris@392 120 if (height < 370) {
Chris@392 121 if (available.height() > 500) height = 370;
Chris@392 122 }
Chris@392 123 if (width < 500) {
Chris@392 124 if (available.width() > 650) width = 500;
Chris@392 125 }
Chris@392 126
Chris@392 127 resize(width, height);
Chris@392 128 }
Chris@392 129
Chris@392 130 ModelDataTableDialog::~ModelDataTableDialog()
Chris@392 131 {
Chris@392 132 delete m_table;
Chris@392 133 }
Chris@392 134
Chris@393 135 void
Chris@394 136 ModelDataTableDialog::scrollToFrameRequested(unsigned long frame)
Chris@393 137 {
Chris@393 138 m_tableView->scrollTo(m_table->getModelIndexForFrame(frame));
Chris@393 139 }
Chris@393 140
Chris@393 141 void
Chris@393 142 ModelDataTableDialog::viewClicked(const QModelIndex &index)
Chris@393 143 {
Chris@393 144 std::cerr << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << std::endl;
Chris@394 145 emit scrollToFrame(m_table->getFrameForModelIndex(index));
Chris@393 146 }
Chris@393 147
Chris@393 148 void
Chris@393 149 ModelDataTableDialog::viewPressed(const QModelIndex &index)
Chris@393 150 {
Chris@393 151 std::cerr << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << std::endl;
Chris@393 152 }
Chris@393 153
Chris@393 154 void
Chris@400 155 ModelDataTableDialog::currentChanged(const QModelIndex &current,
Chris@400 156 const QModelIndex &previous)
Chris@399 157 {
Chris@400 158 std::cerr << "ModelDataTableDialog::currentChanged: from "
Chris@400 159 << previous.row() << ", " << previous.column()
Chris@400 160 << " to " << current.row() << ", " << current.column()
Chris@400 161 << std::endl;
Chris@400 162 m_currentRow = current.row();
Chris@399 163 }
Chris@399 164
Chris@399 165 void
Chris@400 166 ModelDataTableDialog::insertRow()
Chris@399 167 {
Chris@400 168 m_table->insertRow(m_currentRow);
Chris@400 169 }
Chris@400 170
Chris@400 171 void
Chris@400 172 ModelDataTableDialog::deleteRows()
Chris@400 173 {
Chris@400 174 // not efficient
Chris@400 175 while (m_tableView->selectionModel()->hasSelection()) {
Chris@400 176 m_table->removeRow
Chris@400 177 (m_tableView->selectionModel()->selection().indexes().begin()->row());
Chris@400 178 }
Chris@399 179 }
Chris@399 180
Chris@399 181 void
Chris@399 182 ModelDataTableDialog::editRow()
Chris@399 183 {
Chris@399 184 }
Chris@399 185
Chris@399 186 void
Chris@400 187 ModelDataTableDialog::addCommand(Command *command)
Chris@393 188 {
Chris@397 189 CommandHistory::getInstance()->addCommand(command, false, true);
Chris@393 190 }
Chris@393 191