1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* rowviewer.cpp
*
* (c) 2002-2004,2010-2011 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* 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.
*/
/** @file rowviewer.cpp
* Source file for RowViewer
*/
#include <QAbstractButton>
#include <QApplication>
#include <QComboBox>
#include <QClipboard>
#include <QIcon>
#include <QKeyEvent>
#include <QMessageBox>
#include <QTextDocument>
#include <QTextEdit>
#include <QUrl>
#include "database.h"
#include "datatypes.h"
#include "factory.h"
#include "formatting.h"
#include "menuactions.h"
#include "portabase.h"
#include "rowviewer.h"
#include "view.h"
#include "viewdisplay.h"
#include "image/imageutils.h"
/**
* Constructor.
*
* @param dbase The database being viewed
* @param parent The view display widget which is this dialog's parent
*/
RowViewer::RowViewer(Database *dbase, ViewDisplay *parent)
: PBDialog(tr("Row Viewer"), parent), db(dbase), display(parent), currentView(0)
{
tv = Factory::textDisplay(this);
// Make the boolean value images available in case we need them
tv->document()->addResource(QTextDocument::ImageResource,
QUrl("img://icons/checked.png"),
QPixmap(":/icons/checked.png"));
tv->document()->addResource(QTextDocument::ImageResource,
QUrl("img://icons/unchecked.png"),
QPixmap(":/icons/unchecked.png"));
vbox->addWidget(tv);
QHBoxLayout *hbox = Factory::hBoxLayout(vbox);
prevButton = Factory::button(this);
prevButton->setIcon(QIcon(":/icons/back.png"));
prevButton->setToolTip(tr("Previous row"));
connect(prevButton, SIGNAL(clicked()), this, SLOT(previousRow()));
hbox->addWidget(prevButton);
QAbstractButton *editButton = Factory::button(this);
editButton->setIcon(QIcon(":/icons/edit.png"));
editButton->setToolTip(tr("Edit this row"));
connect(editButton, SIGNAL(clicked()), this, SLOT(editRow()));
hbox->addWidget(editButton);
QAbstractButton *copyButton = Factory::button(this);
copyButton->setIcon(QIcon(":/icons/copy_row.png"));
copyButton->setToolTip(tr("Copy this row"));
connect(copyButton, SIGNAL(clicked()), this, SLOT(copyRow()));
hbox->addWidget(copyButton);
QAbstractButton *deleteButton = Factory::button(this);
deleteButton->setIcon(QIcon(":/icons/delete.png"));
deleteButton->setToolTip(tr("Delete this row"));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteRow()));
hbox->addWidget(deleteButton);
QStringList viewNames = db->listViews();
viewNames.removeAll("_all");
viewNames.prepend(MenuActions::tr("All Columns"));
viewBox = new QComboBox(this);
viewBox->addItems(viewNames);
connect(viewBox, SIGNAL(activated(int)), this, SLOT(viewChanged(int)));
hbox->addWidget(viewBox, 1);
#if !defined(Q_WS_MAEMO_5)
// kinetic scrolling takes precedence over text copy on Fremantle
QAbstractButton *copyTextButton = Factory::button(this);
copyTextButton->setIcon(QIcon(":/icons/copy_text.png"));
copyTextButton->setToolTip(tr("Copy the selected text"));
connect(copyTextButton, SIGNAL(clicked()), tv, SLOT(copy()));
hbox->addWidget(copyTextButton);
#endif
nextButton = Factory::button(this);
nextButton->setIcon(QIcon(":/icons/forward.png"));
nextButton->setToolTip(tr("Next row"));
connect(nextButton, SIGNAL(clicked()), this, SLOT(nextRow()));
hbox->addWidget(nextButton);
finishLayout(true, false);
tv->setFocus();
}
/**
* Destructor.
*/
RowViewer::~RowViewer()
{
if (currentView) {
delete currentView;
}
}
/**
* Launch this dialog to view a particular record.
*
* @param originalView The currently selected view
* @param rowIndex Index within the current filter of the record to view
*/
void RowViewer::viewRow(View *originalView, int rowIndex)
{
view = originalView;
if (currentView) {
delete currentView;
}
QString viewName = view->getName();
currentView = db->getView(viewName, false, false);
currentView->copyStateFrom(view);
if (viewName == "_all") {
viewBox->setCurrentIndex(0);
}
else {
int count = viewBox->count();
for (int i = 1; i < count; i++) {
if (viewBox->itemText(i) == viewName) {
viewBox->setCurrentIndex(i);
break;
}
}
}
index = rowIndex;
rowCount = view->totalRowCount();
updateContent();
exec();
}
/**
* Update the display based on which record is now to be shown, etc.
*/
void RowViewer::updateContent()
{
prevButton->setEnabled(index != 0);
nextButton->setEnabled(index != rowCount - 1);
QStringList values = currentView->getRow(index);
QStringList colNames = currentView->getColNames();
int *colTypes = currentView->getColTypes();
QString str = "<qt><table cellspacing=0>";
int count = colNames.count();
int imageIndex = 0;
for (int i = 0; i < count; i++) {
if (i % 2 == 0) {
str += "<tr>";
}
else {
str += "<tr bgcolor=\"lightgray\">";
}
str += "<td><font color=#0000ff>";
str += prepareString(colNames[i]);
str += ": </font></td><td valign=\"middle\">";
int type = colTypes[i];
if (type == BOOLEAN) {
if (values[i].toInt()) {
str += "<img src=\"img://icons/checked.png\">";
}
else {
str += "<img src=\"img://icons/unchecked.png\">";
}
}
else if (type == IMAGE) {
QString format = values[i];
if (!format.isEmpty()) {
int rowId = currentView->getId(index);
QString name = colNames[i];
QImage image = ImageUtils::load(db, rowId, name, format);
QString imageId = QString("img://image%1").arg(imageIndex);
tv->document()->addResource(QTextDocument::ImageResource,
QUrl(imageId), image);
str += QString("<img src=\"%1\">").arg(imageId);
if (!usedImageIds.contains(imageId)) {
usedImageIds.append(imageId);
}
imageIndex++;
}
}
else {
str += prepareString(values[i]);
}
str += "</td></tr>";
}
str += "</table></qt>";
tv->setHtml(str);
tv->setFocus();
}
/**
* Format the provided field value for insertion into an HTML table cell.
*
* @param content The field value from the database
* @return An HTML fragment suitable for use in the display widget
*/
QString RowViewer::prepareString(const QString &content)
{
QString result = "";
int length = content.length();
for (int i = 0; i < length; i++) {
const QChar c = content[i];
if (c == '\n') {
result += "<br>";
}
else if (c == '<') {
result += "<";
}
else if (c == '>') {
result += ">";
}
else if (c == '&') {
result += "&";
}
else {
result += c;
}
}
return result;
}
/**
* Display the next record in the current filter. Called when the next record
* button is clicked.
*/
void RowViewer::nextRow()
{
index++;
updateContent();
}
/**
* Display the previous record in the current filter. Called when the
* previous record button is clicked.
*/
void RowViewer::previousRow()
{
index--;
updateContent();
}
/**
* Launch the row editor to edit the currently displayed record.
*/
void RowViewer::editRow()
{
int rowId = view->getId(index);
if (display->editRow(rowId, false, this)) {
accept();
}
else {
tv->setFocus();
}
}
/**
* Launch the row editor dialog with a copy of the currently displayed record.
*/
void RowViewer::copyRow()
{
int rowId = view->getId(index);
if (display->editRow(rowId, true, this)) {
accept();
}
else {
tv->setFocus();
}
}
/**
* Delete the currently displayed record and return to the data viwer.
*/
void RowViewer::deleteRow()
{
QSettings settings;
bool confirmDeletions = settings.value("General/ConfirmDeletions",
true).toBool();
if (confirmDeletions) {
int choice = QMessageBox::warning(this, qApp->applicationName(),
PortaBase::tr("Delete this row?"),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (choice != QMessageBox::Yes) {
return;
}
}
display->deleteRow();
accept();
}
/**
* Update the data display to reflect a change in the selected database view.
*
* @param index The index of the newly selected view in the selection list
*/
void RowViewer::viewChanged(int index)
{
if (currentView) {
delete currentView;
}
QString name = viewBox->itemText(index);
if (index == 0) {
name = "_all";
}
currentView = db->getView(name, false, false);
currentView->copyStateFrom(view);
updateContent();
}
/**
* Handler for keyboard key release events. Ensures that pressing the left
* and right arrows navigate to the previous and next records in the filter,
* respectively.
*
* @param e The keyboard event which occurred
*/
void RowViewer::keyReleaseEvent(QKeyEvent *e)
{
int key = e->key();
if (key == Qt::Key_Left) {
if (index != 0) {
previousRow();
}
}
else if (key == Qt::Key_Right) {
if (index != rowCount - 1) {
nextRow();
}
}
else if (key == Qt::Key_Space) {
editRow();
}
else {
e->ignore();
}
}
|