annotate widgets/ModelDataTableDialog.cpp @ 494:b3140e9e0665

* Some fairly simplistic code to set up layer type properties based on RDF data about feature types (both when running transforms and when importing features from RDF files).
author Chris Cannam
date Thu, 12 Feb 2009 15:26:43 +0000
parents 55cdd79606ba
children 2e8194a30f40
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@404 37 ModelDataTableDialog::ModelDataTableDialog(TabularModel *model,
Chris@404 38 QString title, QWidget *parent) :
Chris@400 39 QMainWindow(parent),
Chris@401 40 m_currentRow(0),
Chris@404 41 m_trackPlayback(true)
Chris@392 42 {
Chris@392 43 setWindowTitle(tr("Data Editor"));
Chris@392 44
Chris@404 45 QToolBar *toolbar;
Chris@399 46
Chris@404 47 toolbar = addToolBar(tr("Playback Toolbar"));
Chris@404 48 m_playToolbar = toolbar;
Chris@404 49 toolbar = addToolBar(tr("Play Mode Toolbar"));
Chris@404 50
Chris@399 51 IconLoader il;
Chris@399 52
Chris@402 53 QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
Chris@402 54 action->setStatusTip(tr("Toggle tracking of playback position"));
Chris@402 55 action->setCheckable(true);
Chris@404 56 action->setChecked(m_trackPlayback);
Chris@402 57 connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
Chris@402 58 toolbar->addAction(action);
Chris@402 59
Chris@404 60 toolbar = addToolBar(tr("Edit Toolbar"));
Chris@402 61
Chris@402 62 action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
Chris@399 63 action->setShortcut(tr("Insert"));
Chris@399 64 action->setStatusTip(tr("Insert a new item"));
Chris@399 65 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
Chris@399 66 toolbar->addAction(action);
Chris@399 67
Chris@399 68 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
Chris@399 69 action->setShortcut(tr("Delete"));
Chris@399 70 action->setStatusTip(tr("Delete the selected item or items"));
Chris@400 71 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
Chris@399 72 toolbar->addAction(action);
Chris@399 73
Chris@404 74 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@404 75
Chris@404 76 /*
Chris@399 77 action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
Chris@399 78 action->setShortcut(tr("Edit"));
Chris@399 79 action->setStatusTip(tr("Edit the selected item"));
Chris@399 80 connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
Chris@399 81 toolbar->addAction(action);
Chris@404 82 */
Chris@399 83
Chris@396 84 QFrame *mainFrame = new QFrame;
Chris@396 85 setCentralWidget(mainFrame);
Chris@396 86
Chris@392 87 QGridLayout *grid = new QGridLayout;
Chris@396 88 mainFrame->setLayout(grid);
Chris@392 89
Chris@392 90 QGroupBox *box = new QGroupBox;
Chris@398 91 if (title != "") {
Chris@398 92 box->setTitle(title);
Chris@398 93 } else {
Chris@398 94 box->setTitle(tr("Data in Layer"));
Chris@398 95 }
Chris@392 96 grid->addWidget(box, 0, 0);
Chris@392 97 grid->setRowStretch(0, 15);
Chris@392 98
Chris@392 99 QGridLayout *subgrid = new QGridLayout;
Chris@392 100 box->setLayout(subgrid);
Chris@392 101
Chris@392 102 subgrid->setSpacing(0);
Chris@392 103 subgrid->setMargin(5);
Chris@392 104
Chris@392 105 m_tableView = new QTableView;
Chris@392 106 subgrid->addWidget(m_tableView);
Chris@392 107
Chris@399 108 // m_tableView->verticalHeader()->hide();
Chris@392 109 // m_tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
Chris@395 110 m_tableView->setSortingEnabled(true);
Chris@396 111 m_tableView->sortByColumn(0, Qt::AscendingOrder);
Chris@392 112
Chris@392 113 m_table = new ModelDataTableModel(model);
Chris@392 114 m_tableView->setModel(m_table);
Chris@392 115
Chris@393 116 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
Chris@393 117 this, SLOT(viewClicked(const QModelIndex &)));
Chris@393 118 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
Chris@393 119 this, SLOT(viewPressed(const QModelIndex &)));
Chris@400 120 connect(m_tableView->selectionModel(),
Chris@400 121 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
Chris@400 122 this,
Chris@400 123 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
Chris@400 124 connect(m_table, SIGNAL(addCommand(Command *)),
Chris@400 125 this, SLOT(addCommand(Command *)));
Chris@401 126 connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
Chris@401 127 this, SLOT(currentChangedThroughResort(const QModelIndex &)));
Chris@428 128 connect(m_table, SIGNAL(modelRemoved()),
Chris@428 129 this, SLOT(modelRemoved()));
Chris@393 130
Chris@392 131 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@396 132 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@392 133 grid->addWidget(bb, 2, 0);
Chris@392 134 grid->setRowStretch(2, 0);
Chris@392 135
Chris@392 136 QDesktopWidget *desktop = QApplication::desktop();
Chris@392 137 QRect available = desktop->availableGeometry();
Chris@392 138
Chris@392 139 int width = available.width() / 3;
Chris@392 140 int height = available.height() / 2;
Chris@392 141 if (height < 370) {
Chris@392 142 if (available.height() > 500) height = 370;
Chris@392 143 }
Chris@392 144 if (width < 500) {
Chris@392 145 if (available.width() > 650) width = 500;
Chris@392 146 }
Chris@392 147
Chris@392 148 resize(width, height);
Chris@392 149 }
Chris@392 150
Chris@392 151 ModelDataTableDialog::~ModelDataTableDialog()
Chris@392 152 {
Chris@392 153 delete m_table;
Chris@392 154 }
Chris@392 155
Chris@393 156 void
Chris@401 157 ModelDataTableDialog::userScrolledToFrame(unsigned long frame)
Chris@393 158 {
Chris@401 159 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 160 makeCurrent(index.row());
Chris@401 161 }
Chris@401 162
Chris@401 163 void
Chris@401 164 ModelDataTableDialog::playbackScrolledToFrame(unsigned long frame)
Chris@401 165 {
Chris@401 166 if (m_trackPlayback) {
Chris@401 167 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 168 makeCurrent(index.row());
Chris@401 169 }
Chris@401 170 }
Chris@401 171
Chris@401 172 void
Chris@401 173 ModelDataTableDialog::makeCurrent(int row)
Chris@401 174 {
Chris@401 175 int rh = m_tableView->height() / m_tableView->rowHeight(0);
Chris@404 176 int topRow = row - rh/4;
Chris@401 177 if (topRow < 0) topRow = 0;
Chris@404 178
Chris@404 179 // should only scroll if the desired row is not currently visible
Chris@404 180
Chris@404 181 // should only select if no part of the desired row is currently selected
Chris@404 182
Chris@405 183 // std::cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
Chris@405 184 // << topRow << std::endl;
Chris@404 185
Chris@404 186 int pos = m_tableView->rowViewportPosition(row);
Chris@404 187
Chris@404 188 if (pos < 0 || pos >= m_tableView->height() - rh) {
Chris@404 189 m_tableView->scrollTo(m_table->index(topRow, 0));
Chris@404 190 }
Chris@404 191
Chris@404 192 bool haveRowSelected = false;
Chris@404 193 for (int i = 0; i < m_table->columnCount(); ++i) {
Chris@404 194 if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
Chris@404 195 haveRowSelected = true;
Chris@404 196 break;
Chris@404 197 }
Chris@404 198 }
Chris@404 199
Chris@404 200 if (!haveRowSelected) {
Chris@404 201 m_tableView->selectionModel()->setCurrentIndex
Chris@404 202 (m_table->index(row, 0),
Chris@404 203 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
Chris@404 204 }
Chris@393 205 }
Chris@393 206
Chris@393 207 void
Chris@393 208 ModelDataTableDialog::viewClicked(const QModelIndex &index)
Chris@393 209 {
Chris@409 210 // std::cerr << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << std::endl;
Chris@394 211 emit scrollToFrame(m_table->getFrameForModelIndex(index));
Chris@393 212 }
Chris@393 213
Chris@393 214 void
Chris@393 215 ModelDataTableDialog::viewPressed(const QModelIndex &index)
Chris@393 216 {
Chris@409 217 // std::cerr << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << std::endl;
Chris@393 218 }
Chris@393 219
Chris@393 220 void
Chris@400 221 ModelDataTableDialog::currentChanged(const QModelIndex &current,
Chris@400 222 const QModelIndex &previous)
Chris@399 223 {
Chris@409 224 // std::cerr << "ModelDataTableDialog::currentChanged: from "
Chris@409 225 // << previous.row() << ", " << previous.column()
Chris@409 226 // << " to " << current.row() << ", " << current.column()
Chris@409 227 // << std::endl;
Chris@400 228 m_currentRow = current.row();
Chris@401 229 m_table->setCurrentRow(m_currentRow);
Chris@399 230 }
Chris@399 231
Chris@399 232 void
Chris@400 233 ModelDataTableDialog::insertRow()
Chris@399 234 {
Chris@400 235 m_table->insertRow(m_currentRow);
Chris@400 236 }
Chris@400 237
Chris@400 238 void
Chris@400 239 ModelDataTableDialog::deleteRows()
Chris@400 240 {
Chris@400 241 // not efficient
Chris@400 242 while (m_tableView->selectionModel()->hasSelection()) {
Chris@400 243 m_table->removeRow
Chris@400 244 (m_tableView->selectionModel()->selection().indexes().begin()->row());
Chris@400 245 }
Chris@399 246 }
Chris@399 247
Chris@399 248 void
Chris@399 249 ModelDataTableDialog::editRow()
Chris@399 250 {
Chris@399 251 }
Chris@399 252
Chris@399 253 void
Chris@400 254 ModelDataTableDialog::addCommand(Command *command)
Chris@393 255 {
Chris@397 256 CommandHistory::getInstance()->addCommand(command, false, true);
Chris@393 257 }
Chris@393 258
Chris@401 259 void
Chris@402 260 ModelDataTableDialog::togglePlayTracking()
Chris@402 261 {
Chris@402 262 m_trackPlayback = !m_trackPlayback;
Chris@402 263 }
Chris@402 264
Chris@402 265 void
Chris@401 266 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
Chris@401 267 {
Chris@409 268 // std::cerr << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << std::endl;
Chris@401 269 // m_tableView->scrollTo(index);
Chris@401 270 makeCurrent(index.row());
Chris@401 271 }
Chris@401 272
Chris@428 273 void
Chris@428 274 ModelDataTableDialog::modelRemoved()
Chris@428 275 {
Chris@428 276 close();
Chris@428 277 }
Chris@401 278
Chris@401 279