annotate widgets/ImageDialog.cpp @ 304:4b7e8da8f069

* More work on image layer display &c
author Chris Cannam
date Fri, 05 Oct 2007 13:27:21 +0000
parents 46faec7aae12
children 013a37723c0a
rev   line source
Chris@303 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@303 2
Chris@303 3 /*
Chris@303 4 Sonic Visualiser
Chris@303 5 An audio file viewer and annotation editor.
Chris@303 6 Centre for Digital Music, Queen Mary, University of London.
Chris@303 7 This file copyright 2007 QMUL.
Chris@303 8
Chris@303 9 This program is free software; you can redistribute it and/or
Chris@303 10 modify it under the terms of the GNU General Public License as
Chris@303 11 published by the Free Software Foundation; either version 2 of the
Chris@303 12 License, or (at your option) any later version. See the file
Chris@303 13 COPYING included with this distribution for more information.
Chris@303 14 */
Chris@303 15
Chris@303 16 #include "ImageDialog.h"
Chris@303 17
Chris@303 18 #include <QLineEdit>
Chris@303 19 #include <QGridLayout>
Chris@303 20 #include <QLabel>
Chris@303 21 #include <QDialogButtonBox>
Chris@303 22 #include <QPushButton>
Chris@303 23 #include <QGroupBox>
Chris@304 24 #include <QDesktopWidget>
Chris@304 25 #include <QApplication>
Chris@303 26
Chris@303 27 #include "data/fileio/FileFinder.h"
Chris@303 28
Chris@303 29 ImageDialog::ImageDialog(QString title,
Chris@303 30 QString image,
Chris@303 31 QString label,
Chris@303 32 QWidget *parent) :
Chris@303 33 QDialog(parent),
Chris@303 34 m_imagePreview(0)
Chris@303 35 {
Chris@303 36 setWindowTitle(title);
Chris@303 37
Chris@303 38 QGridLayout *grid = new QGridLayout;
Chris@303 39 setLayout(grid);
Chris@303 40
Chris@303 41 QGroupBox *databox = new QGroupBox(tr("Image"));
Chris@303 42
Chris@303 43 QGridLayout *subgrid = new QGridLayout;
Chris@303 44 databox->setLayout(subgrid);
Chris@303 45
Chris@303 46 int row = 0;
Chris@303 47
Chris@303 48 subgrid->addWidget(new QLabel(tr("Label:")), row, 0);
Chris@303 49
Chris@303 50 m_labelEdit = new QLineEdit;
Chris@303 51 subgrid->addWidget(m_labelEdit, row, 1, 1, 2);
Chris@303 52
Chris@303 53 ++row;
Chris@303 54
Chris@303 55 subgrid->addWidget(new QLabel(tr("File:")), row, 0);
Chris@303 56
Chris@303 57 m_imageEdit = new QLineEdit;
Chris@303 58 subgrid->addWidget(m_imageEdit, row, 1, 1, 1);
Chris@304 59 connect(m_imageEdit, SIGNAL(textEdited(const QString &)),
Chris@304 60 this, SLOT(imageEditEdited(const QString &)));
Chris@303 61
Chris@303 62 QPushButton *browse = new QPushButton(tr("Browse..."));
Chris@303 63 connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked()));
Chris@303 64 subgrid->addWidget(browse, row, 2, 1, 1);
Chris@303 65
Chris@303 66 ++row;
Chris@303 67
Chris@303 68 QGroupBox *previewbox = new QGroupBox(tr("Preview"));
Chris@303 69
Chris@303 70 subgrid = new QGridLayout;
Chris@303 71 previewbox->setLayout(subgrid);
Chris@303 72
Chris@303 73 m_imagePreview = new QLabel;
Chris@303 74 m_imagePreview->setAlignment(Qt::AlignCenter);
Chris@303 75 subgrid->addWidget(m_imagePreview, 0, 0);
Chris@303 76
Chris@303 77 m_imagePreview->setMinimumSize(QSize(100, 100));
Chris@303 78
Chris@304 79 QDesktopWidget *desktop = QApplication::desktop();
Chris@304 80 m_imagePreview->setMaximumSize(QSize((desktop->width() * 2) / 3,
Chris@304 81 (desktop->height() * 2) / 3));
Chris@304 82
Chris@303 83 grid->addWidget(databox, 0, 0);
Chris@303 84 grid->addWidget(previewbox, 1, 0);
Chris@303 85
Chris@303 86 grid->setRowStretch(1, 10);
Chris@303 87
Chris@303 88 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@303 89 QDialogButtonBox::Cancel);
Chris@303 90 grid->addWidget(bb, 2, 0, 1, 1);
Chris@303 91 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@303 92 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@303 93
Chris@303 94 m_okButton = bb->button(QDialogButtonBox::Ok);
Chris@303 95 m_okButton->setEnabled(false);
Chris@303 96
Chris@303 97 if (image != "") setImage(image);
Chris@303 98 if (label != "") setLabel(label);
Chris@303 99 }
Chris@303 100
Chris@303 101 ImageDialog::~ImageDialog()
Chris@303 102 {
Chris@303 103 }
Chris@303 104
Chris@303 105 QString
Chris@303 106 ImageDialog::getImage()
Chris@303 107 {
Chris@303 108 return m_loadedImageFile;
Chris@303 109 }
Chris@303 110
Chris@303 111 QPixmap
Chris@303 112 ImageDialog::getPixmap()
Chris@303 113 {
Chris@303 114 return m_loadedImage;
Chris@303 115 }
Chris@303 116
Chris@303 117 QString
Chris@303 118 ImageDialog::getLabel()
Chris@303 119 {
Chris@303 120 return m_labelEdit->text();
Chris@303 121 }
Chris@303 122
Chris@303 123 void
Chris@303 124 ImageDialog::setImage(QString image)
Chris@303 125 {
Chris@303 126 m_imageEdit->setText(image);
Chris@303 127 updatePreview();
Chris@303 128 }
Chris@303 129
Chris@303 130 void
Chris@303 131 ImageDialog::setLabel(QString label)
Chris@303 132 {
Chris@303 133 m_labelEdit->setText(label);
Chris@303 134 }
Chris@303 135
Chris@303 136 void
Chris@303 137 ImageDialog::resizeEvent(QResizeEvent *)
Chris@303 138 {
Chris@303 139 updatePreview();
Chris@303 140 }
Chris@303 141
Chris@303 142 void
Chris@304 143 ImageDialog::imageEditEdited(const QString &)
Chris@304 144 {
Chris@304 145 updatePreview();
Chris@304 146 }
Chris@304 147
Chris@304 148 void
Chris@303 149 ImageDialog::updatePreview()
Chris@303 150 {
Chris@303 151 if (!m_imagePreview) return;
Chris@303 152
Chris@303 153 QString img = m_imageEdit->text();
Chris@303 154
Chris@304 155 m_okButton->setEnabled(img != "");
Chris@304 156
Chris@303 157 if (img != m_loadedImageFile) {
Chris@303 158 m_loadedImage = QPixmap(img);
Chris@303 159 m_loadedImageFile = img;
Chris@303 160 }
Chris@303 161
Chris@303 162 QSize sz(m_imagePreview->size());
Chris@303 163 int m = m_imagePreview->margin() * 2;
Chris@303 164 sz -= QSize(m, m);
Chris@303 165
Chris@303 166 if (m_loadedImage.isNull()) {
Chris@303 167 m_imagePreview->setPixmap(QPixmap());
Chris@303 168 } else {
Chris@303 169 m_imagePreview->setPixmap(m_loadedImage.scaled
Chris@303 170 (sz,
Chris@303 171 Qt::KeepAspectRatio,
Chris@303 172 Qt::SmoothTransformation));
Chris@303 173 }
Chris@303 174 }
Chris@303 175
Chris@303 176 void
Chris@303 177 ImageDialog::browseClicked()
Chris@303 178 {
Chris@303 179 QString file =
Chris@303 180 FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile);
Chris@303 181
Chris@303 182 if (file != "") {
Chris@303 183 setImage(file);
Chris@303 184 emit imageChanged(file);
Chris@303 185 }
Chris@303 186 }
Chris@303 187
Chris@303 188
Chris@303 189