svgui  1.9
ModelDataTableDialog.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2008 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "ModelDataTableDialog.h"
17 
18 #include "data/model/ModelDataTableModel.h"
19 #include "data/model/TabularModel.h"
20 #include "data/model/Model.h"
21 
22 #include "CommandHistory.h"
23 #include "IconLoader.h"
24 
25 #include <QTableView>
26 #include <QLineEdit>
27 #include <QGridLayout>
28 #include <QLabel>
29 #include <QGroupBox>
30 #include <QDialogButtonBox>
31 #include <QHeaderView>
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QAction>
35 #include <QToolBar>
36 
37 #include <iostream>
38 
40  QString title, QWidget *parent) :
41  QMainWindow(parent),
42  m_currentRow(0),
43  m_trackPlayback(true)
44 {
45  setWindowTitle(tr("Data Editor"));
46 
47  QToolBar *toolbar;
48 
49  toolbar = addToolBar(tr("Playback Toolbar"));
50  m_playToolbar = toolbar;
51  toolbar = addToolBar(tr("Play Mode Toolbar"));
52 
53  IconLoader il;
54 
55  QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
56  action->setStatusTip(tr("Toggle tracking of playback position"));
57  action->setCheckable(true);
58  action->setChecked(m_trackPlayback);
59  connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
60  toolbar->addAction(action);
61 
62  toolbar = addToolBar(tr("Edit Toolbar"));
63 
64  action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
65  action->setShortcut(tr("Insert"));
66  action->setStatusTip(tr("Insert a new item"));
67  connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
68  toolbar->addAction(action);
69 
70  action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
71  action->setShortcut(tr("Delete"));
72  action->setStatusTip(tr("Delete the selected item or items"));
73  connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
74  toolbar->addAction(action);
75 
77 
78 /*
79  action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
80  action->setShortcut(tr("Edit"));
81  action->setStatusTip(tr("Edit the selected item"));
82  connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
83  toolbar->addAction(action);
84 */
85 
86  QFrame *mainFrame = new QFrame;
87  setCentralWidget(mainFrame);
88 
89  QGridLayout *grid = new QGridLayout;
90  mainFrame->setLayout(grid);
91 
92  QGroupBox *box = new QGroupBox;
93  if (title != "") {
94  box->setTitle(title);
95  } else {
96  box->setTitle(tr("Data in Layer"));
97  }
98  grid->addWidget(box, 0, 0);
99  grid->setRowStretch(0, 15);
100 
101  QGridLayout *subgrid = new QGridLayout;
102  box->setLayout(subgrid);
103 
104  subgrid->setSpacing(0);
105  subgrid->setMargin(5);
106 
107  subgrid->addWidget(new QLabel(tr("Find:")), 1, 0);
108  subgrid->addWidget(new QLabel(tr(" ")), 1, 1);
109  m_find = new QLineEdit;
110  subgrid->addWidget(m_find, 1, 2);
111  connect(m_find, SIGNAL(textChanged(const QString &)),
112  this, SLOT(searchTextChanged(const QString &)));
113  connect(m_find, SIGNAL(returnPressed()),
114  this, SLOT(searchRepeated()));
115 
116  m_tableView = new QTableView;
117  subgrid->addWidget(m_tableView, 0, 0, 1, 3);
118 
119  m_tableView->setSortingEnabled(true);
120  m_tableView->sortByColumn(0, Qt::AscendingOrder);
121 
122  m_table = new ModelDataTableModel(model);
123  m_tableView->setModel(m_table);
124 
125  m_tableView->horizontalHeader()->setStretchLastSection(true);
126 
127  connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
128  this, SLOT(viewClicked(const QModelIndex &)));
129  connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
130  this, SLOT(viewPressed(const QModelIndex &)));
131  connect(m_tableView->selectionModel(),
132  SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
133  this,
134  SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
135  connect(m_table, SIGNAL(addCommand(Command *)),
136  this, SLOT(addCommand(Command *)));
137  connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
138  this, SLOT(currentChangedThroughResort(const QModelIndex &)));
139  connect(m_table, SIGNAL(modelRemoved()),
140  this, SLOT(modelRemoved()));
141 
142  QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
143  connect(bb, SIGNAL(rejected()), this, SLOT(close()));
144  grid->addWidget(bb, 2, 0);
145  grid->setRowStretch(2, 0);
146 
147  QDesktopWidget *desktop = QApplication::desktop();
148  QRect available = desktop->availableGeometry();
149 
150  int width = available.width() / 3;
151  int height = available.height() / 2;
152  if (height < 370) {
153  if (available.height() > 500) height = 370;
154  }
155  if (width < 650) {
156  if (available.width() > 750) width = 650;
157  else if (width < 500) {
158  if (available.width() > 650) width = 500;
159  }
160  }
161 
162  resize(width, height);
163 }
164 
166 {
167  delete m_table;
168 }
169 
170 void
172 {
173  QModelIndex index = m_table->getModelIndexForFrame(frame);
174  makeCurrent(index.row());
175 }
176 
177 void
179 {
180  if (m_trackPlayback) {
181  QModelIndex index = m_table->getModelIndexForFrame(frame);
182  makeCurrent(index.row());
183  }
184 }
185 
186 void
188 {
189  QModelIndex mi = m_table->findText(text);
190  if (mi.isValid()) {
191  makeCurrent(mi.row());
192  m_tableView->selectionModel()->setCurrentIndex
193  (mi, QItemSelectionModel::ClearAndSelect);
194  }
195 }
196 
197 void
199 {
200  QModelIndex mi = m_table->findText(m_find->text());
201  if (mi.isValid()) {
202  makeCurrent(mi.row());
203  m_tableView->selectionModel()->setCurrentIndex
204  (mi, QItemSelectionModel::ClearAndSelect);
205  }
206 }
207 
208 void
210 {
211  int rh = m_tableView->height() / m_tableView->rowHeight(0);
212  int topRow = row - rh/4;
213  if (topRow < 0) topRow = 0;
214 
215  // should only scroll if the desired row is not currently visible
216 
217  // should only select if no part of the desired row is currently selected
218 
219 // cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
220 // << topRow << endl;
221 
222  int pos = m_tableView->rowViewportPosition(row);
223 
224  if (pos < 0 || pos >= m_tableView->height() - rh) {
225  m_tableView->scrollTo(m_table->index(topRow, 0));
226  }
227 
228  bool haveRowSelected = false;
229  for (int i = 0; i < m_table->columnCount(); ++i) {
230  if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
231  haveRowSelected = true;
232  break;
233  }
234  }
235 
236  if (!haveRowSelected) {
237  m_tableView->selectionModel()->setCurrentIndex
238  (m_table->index(row, 0),
239  QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
240  }
241 }
242 
243 void
244 ModelDataTableDialog::viewClicked(const QModelIndex &index)
245 {
246 // SVDEBUG << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << endl;
247  emit scrollToFrame(m_table->getFrameForModelIndex(index));
248 }
249 
250 void
252 {
253 // SVDEBUG << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << endl;
254 }
255 
256 void
257 ModelDataTableDialog::currentChanged(const QModelIndex &current,
258  const QModelIndex &)
259 {
260 // SVDEBUG << "ModelDataTableDialog::currentChanged: from "
261 // << previous.row() << ", " << previous.column()
262 // << " to " << current.row() << ", " << current.column()
263 // << endl;
264  m_currentRow = current.row();
265  m_table->setCurrentRow(m_currentRow);
266 }
267 
268 void
270 {
271  m_table->insertRow(m_currentRow);
272 }
273 
274 void
276 {
277  // not efficient
278  while (m_tableView->selectionModel()->hasSelection()) {
279  m_table->removeRow
280  (m_tableView->selectionModel()->selection().indexes().begin()->row());
281  }
282 }
283 
284 void
286 {
287 }
288 
289 void
291 {
292  CommandHistory::getInstance()->addCommand(command, false, true);
293 }
294 
295 void
297 {
299 }
300 
301 void
303 {
304 // SVDEBUG << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << endl;
305 // m_tableView->scrollTo(index);
306  makeCurrent(index.row());
307 }
308 
309 void
311 {
312  close();
313 }
314 
315 
void playbackScrolledToFrame(int frame)
void currentChangedThroughResort(const QModelIndex &)
void addCommand(Command *command)
Add a command to the command history.
void registerToolbar(QToolBar *toolbar)
void scrollToFrame(int frame)
static CommandHistory * getInstance()
void viewPressed(const QModelIndex &)
void viewClicked(const QModelIndex &)
void userScrolledToFrame(int frame)
ModelDataTableModel * m_table
QIcon load(QString name)
Definition: IconLoader.cpp:55
void currentChanged(const QModelIndex &, const QModelIndex &)
ModelDataTableDialog(TabularModel *model, QString title, QWidget *parent=0)
void searchTextChanged(const QString &)