File: imageselector.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (184 lines) | stat: -rw-r--r-- 5,206 bytes parent folder | download | duplicates (2)
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
/*
 * imageselector.cpp
 *
 * (c) 2003-2004,2008-2010 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 imageselector.cpp
 * Source file for ImageSelector
 */

#include <QApplication>
#include <QBuffer>
#include <QFile>
#include <QImage>
#include <QMessageBox>
#include <QPushButton>
#include "../database.h"
#include "../factory.h"
#include "../importdialog.h"
#include "imageeditor.h"
#include "imageselector.h"
#include "imageutils.h"
#include "imageviewer.h"

/**
 * Constructor.
 *
 * @param dbase The database being edited
 * @param parent This widget's parent widget
 */
ImageSelector::ImageSelector(Database *dbase, QWidget *parent)
    : QStackedWidget(parent), db(dbase), format(""), changed(false)
{
    selectButton = new QPushButton(tr("Select an image"), this);
    addWidget(selectButton);
    connect(selectButton, SIGNAL(clicked()), this, SLOT(selectImage()));
    setMaximumHeight(selectButton->sizeHint().height());

    editButtons = new QWidget(this);
    QHBoxLayout *layout = Factory::hBoxLayout(editButtons, true);
    QPushButton *viewButton = new QPushButton(tr("View"), editButtons);
    layout->addWidget(viewButton);
    connect(viewButton, SIGNAL(clicked()), this, SLOT(viewImage()));
    QPushButton *changeButton = new QPushButton(tr("Change"), editButtons);
    layout->addWidget(changeButton);
    connect(changeButton, SIGNAL(clicked()), this, SLOT(selectImage()));
    QPushButton *deleteButton = new QPushButton(tr("Delete"), editButtons);
    layout->addWidget(deleteButton);
    connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteImage()));
    addWidget(editButtons);
    setCurrentWidget(selectButton);
}

/**
 * Specify the field that this widget is to represent.
 *
 * @param row The ID of the row containing the field (-1 if new)
 * @param column The name of the column containing the field
 */
void ImageSelector::setField(int row, const QString &column)
{
    rowId = row;
    colName = column;
    if (rowId == -1) {
        changed = true;
    }
}

/**
 * Get the format of the currently selected image.  Valid values are "" (if
 * no image has been selected), "JPEG", or "PNG".
 *
 * @return The format of the selected image
 */
QString ImageSelector::getFormat()
{
    return format;
}

/**
 * Set the format of the currently selected image.  Called by the RowEditor so
 * that the format of image data already in the database is known.
 *
 * @param newFormat The format of the image data in this field
 */
void ImageSelector::setFormat(const QString &newFormat)
{
    format = newFormat;
    if (!format.isEmpty()) {
        setCurrentWidget(editButtons);
    }
}

/**
 * Select a file from which to load an image.  Called when the "Select an
 * image" button is clicked.
 */
void ImageSelector::selectImage()
{
    ImportDialog dialog(ImportDialog::Image, this);
    if (!dialog.exec()) {
        return;
    }
    if (!dialog.import(db)) {
        return;
    }
    QString file = dialog.getPath();
    ImageEditor editor(this);
    if (!editor.edit(file)) {
        return;
    }
    format = editor.getFormat();
    image = editor.getImage();
    path = editor.isModified() ? QString("") : file;
    changed = true;
    setCurrentWidget(editButtons);
}

/**
 * Launch a dialog to display the currently selected image.  Called when the
 * "View" button is clicked.
 */
void ImageSelector::viewImage()
{
    if (image.isNull()) {
        image = ImageUtils::load(db, rowId, colName, format);
    }
    ImageViewer viewer(false, this);
    viewer.setImage(image);
    viewer.exec();
}

/**
 * Delete the currently selected image from the current database field,
 * setting it back to "no image selected".
 */
void ImageSelector::deleteImage()
{
    int choice = QMessageBox::warning(this, qApp->applicationName(),
                                      tr("Delete the current image?"),
                                      QMessageBox::Yes|QMessageBox::No,
                                      QMessageBox::No);
    if (choice != QMessageBox::Yes) {
        return;
    }
    image = QImage();
    format = "";
    changed = true;
    setCurrentWidget(selectButton);
}

/**
 * Save the field's image data to the database.
 *
 * @param id The ID of the row being saved to
 */
void ImageSelector::saveImage(int id)
{
    if (!changed) {
        return;
    }
    if (!path.isEmpty()) {
        // The image wasn't modified, so just copy the file data
        QFile file(path);
        file.open(QIODevice::ReadOnly);
        QByteArray bytes = file.readAll();
        file.close();
        db->setBinaryField(id, colName, bytes);
    }
    else if (!format.isEmpty()) {
        // The image was modified, so need to encode the in-memory image data
        QByteArray bytes;
        QBuffer buffer(&bytes);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer, format.toLower().toLatin1());
        buffer.close();
        db->setBinaryField(id, colName, bytes);
    }
}