annotate widgets/ModelDataTableDialog.cpp @ 403:dc32f6e7839b

* minor enhancement to data edit make-current
author Chris Cannam
date Wed, 18 Jun 2008 14:34:13 +0000
parents 66e01a6c9554
children 4075bf03faef
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@401 39 m_currentRow(0),
Chris@401 40 m_trackPlayback(false)
Chris@392 41 {
Chris@392 42 setWindowTitle(tr("Data Editor"));
Chris@392 43
Chris@396 44 QToolBar *toolbar = addToolBar(tr("Toolbar"));
Chris@399 45
Chris@399 46 IconLoader il;
Chris@399 47
Chris@402 48 QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
Chris@402 49 action->setStatusTip(tr("Toggle tracking of playback position"));
Chris@402 50 action->setCheckable(true);
Chris@402 51 connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
Chris@402 52 toolbar->addAction(action);
Chris@402 53
Chris@402 54 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@402 55
Chris@402 56 action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
Chris@399 57 action->setShortcut(tr("Insert"));
Chris@399 58 action->setStatusTip(tr("Insert a new item"));
Chris@399 59 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
Chris@399 60 toolbar->addAction(action);
Chris@399 61
Chris@399 62 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
Chris@399 63 action->setShortcut(tr("Delete"));
Chris@399 64 action->setStatusTip(tr("Delete the selected item or items"));
Chris@400 65 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
Chris@399 66 toolbar->addAction(action);
Chris@399 67
Chris@399 68 action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
Chris@399 69 action->setShortcut(tr("Edit"));
Chris@399 70 action->setStatusTip(tr("Edit the selected item"));
Chris@399 71 connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
Chris@399 72 toolbar->addAction(action);
Chris@399 73
Chris@396 74 QFrame *mainFrame = new QFrame;
Chris@396 75 setCentralWidget(mainFrame);
Chris@396 76
Chris@392 77 QGridLayout *grid = new QGridLayout;
Chris@396 78 mainFrame->setLayout(grid);
Chris@392 79
Chris@392 80 QGroupBox *box = new QGroupBox;
Chris@398 81 if (title != "") {
Chris@398 82 box->setTitle(title);
Chris@398 83 } else {
Chris@398 84 box->setTitle(tr("Data in Layer"));
Chris@398 85 }
Chris@392 86 grid->addWidget(box, 0, 0);
Chris@392 87 grid->setRowStretch(0, 15);
Chris@392 88
Chris@392 89 QGridLayout *subgrid = new QGridLayout;
Chris@392 90 box->setLayout(subgrid);
Chris@392 91
Chris@392 92 subgrid->setSpacing(0);
Chris@392 93 subgrid->setMargin(5);
Chris@392 94
Chris@392 95 m_tableView = new QTableView;
Chris@392 96 subgrid->addWidget(m_tableView);
Chris@392 97
Chris@399 98 // m_tableView->verticalHeader()->hide();
Chris@392 99 // m_tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
Chris@395 100 m_tableView->setSortingEnabled(true);
Chris@396 101 m_tableView->sortByColumn(0, Qt::AscendingOrder);
Chris@392 102
Chris@392 103 m_table = new ModelDataTableModel(model);
Chris@392 104 m_tableView->setModel(m_table);
Chris@392 105
Chris@393 106 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
Chris@393 107 this, SLOT(viewClicked(const QModelIndex &)));
Chris@393 108 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
Chris@393 109 this, SLOT(viewPressed(const QModelIndex &)));
Chris@400 110 connect(m_tableView->selectionModel(),
Chris@400 111 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
Chris@400 112 this,
Chris@400 113 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
Chris@400 114 connect(m_table, SIGNAL(addCommand(Command *)),
Chris@400 115 this, SLOT(addCommand(Command *)));
Chris@401 116 connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
Chris@401 117 this, SLOT(currentChangedThroughResort(const QModelIndex &)));
Chris@393 118
Chris@392 119 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@396 120 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@392 121 grid->addWidget(bb, 2, 0);
Chris@392 122 grid->setRowStretch(2, 0);
Chris@392 123
Chris@392 124 QDesktopWidget *desktop = QApplication::desktop();
Chris@392 125 QRect available = desktop->availableGeometry();
Chris@392 126
Chris@392 127 int width = available.width() / 3;
Chris@392 128 int height = available.height() / 2;
Chris@392 129 if (height < 370) {
Chris@392 130 if (available.height() > 500) height = 370;
Chris@392 131 }
Chris@392 132 if (width < 500) {
Chris@392 133 if (available.width() > 650) width = 500;
Chris@392 134 }
Chris@392 135
Chris@392 136 resize(width, height);
Chris@392 137 }
Chris@392 138
Chris@392 139 ModelDataTableDialog::~ModelDataTableDialog()
Chris@392 140 {
Chris@392 141 delete m_table;
Chris@392 142 }
Chris@392 143
Chris@393 144 void
Chris@401 145 ModelDataTableDialog::userScrolledToFrame(unsigned long frame)
Chris@393 146 {
Chris@401 147 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 148 makeCurrent(index.row());
Chris@401 149 }
Chris@401 150
Chris@401 151 void
Chris@401 152 ModelDataTableDialog::playbackScrolledToFrame(unsigned long frame)
Chris@401 153 {
Chris@401 154 if (m_trackPlayback) {
Chris@401 155 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 156 makeCurrent(index.row());
Chris@401 157 }
Chris@401 158 }
Chris@401 159
Chris@401 160 void
Chris@401 161 ModelDataTableDialog::makeCurrent(int row)
Chris@401 162 {
Chris@401 163 int rh = m_tableView->height() / m_tableView->rowHeight(0);
Chris@401 164 int topRow = row - rh/2;
Chris@401 165 if (topRow < 0) topRow = 0;
Chris@402 166 //!!! should not do any of this if an item in the given row is
Chris@402 167 //already current; should not scroll if the current row is already
Chris@402 168 //visible
Chris@403 169 std::cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
Chris@403 170 << topRow << std::endl;
Chris@401 171 m_tableView->scrollTo
Chris@401 172 (m_table->getModelIndexForRow(topRow));
Chris@401 173 m_tableView->selectionModel()->setCurrentIndex
Chris@403 174 (m_table->getModelIndexForRow(row),
Chris@403 175 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
Chris@393 176 }
Chris@393 177
Chris@393 178 void
Chris@393 179 ModelDataTableDialog::viewClicked(const QModelIndex &index)
Chris@393 180 {
Chris@393 181 std::cerr << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << std::endl;
Chris@394 182 emit scrollToFrame(m_table->getFrameForModelIndex(index));
Chris@393 183 }
Chris@393 184
Chris@393 185 void
Chris@393 186 ModelDataTableDialog::viewPressed(const QModelIndex &index)
Chris@393 187 {
Chris@393 188 std::cerr << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << std::endl;
Chris@393 189 }
Chris@393 190
Chris@393 191 void
Chris@400 192 ModelDataTableDialog::currentChanged(const QModelIndex &current,
Chris@400 193 const QModelIndex &previous)
Chris@399 194 {
Chris@400 195 std::cerr << "ModelDataTableDialog::currentChanged: from "
Chris@400 196 << previous.row() << ", " << previous.column()
Chris@400 197 << " to " << current.row() << ", " << current.column()
Chris@400 198 << std::endl;
Chris@400 199 m_currentRow = current.row();
Chris@401 200 m_table->setCurrentRow(m_currentRow);
Chris@399 201 }
Chris@399 202
Chris@399 203 void
Chris@400 204 ModelDataTableDialog::insertRow()
Chris@399 205 {
Chris@400 206 m_table->insertRow(m_currentRow);
Chris@400 207 }
Chris@400 208
Chris@400 209 void
Chris@400 210 ModelDataTableDialog::deleteRows()
Chris@400 211 {
Chris@400 212 // not efficient
Chris@400 213 while (m_tableView->selectionModel()->hasSelection()) {
Chris@400 214 m_table->removeRow
Chris@400 215 (m_tableView->selectionModel()->selection().indexes().begin()->row());
Chris@400 216 }
Chris@399 217 }
Chris@399 218
Chris@399 219 void
Chris@399 220 ModelDataTableDialog::editRow()
Chris@399 221 {
Chris@399 222 }
Chris@399 223
Chris@399 224 void
Chris@400 225 ModelDataTableDialog::addCommand(Command *command)
Chris@393 226 {
Chris@397 227 CommandHistory::getInstance()->addCommand(command, false, true);
Chris@393 228 }
Chris@393 229
Chris@401 230 void
Chris@402 231 ModelDataTableDialog::togglePlayTracking()
Chris@402 232 {
Chris@402 233 m_trackPlayback = !m_trackPlayback;
Chris@402 234 }
Chris@402 235
Chris@402 236 void
Chris@401 237 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
Chris@401 238 {
Chris@401 239 std::cerr << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << std::endl;
Chris@401 240 // m_tableView->scrollTo(index);
Chris@401 241 makeCurrent(index.row());
Chris@401 242 }
Chris@401 243
Chris@401 244
Chris@401 245