annotate widgets/ImageDialog.cpp @ 303:46faec7aae12

* Phase 1 of an image layer.
author Chris Cannam
date Thu, 04 Oct 2007 16:34:11 +0000
parents
children 4b7e8da8f069
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@303 24
Chris@303 25 #include "data/fileio/FileFinder.h"
Chris@303 26
Chris@303 27 ImageDialog::ImageDialog(QString title,
Chris@303 28 QString image,
Chris@303 29 QString label,
Chris@303 30 QWidget *parent) :
Chris@303 31 QDialog(parent),
Chris@303 32 m_imagePreview(0)
Chris@303 33 {
Chris@303 34 setWindowTitle(title);
Chris@303 35
Chris@303 36 QGridLayout *grid = new QGridLayout;
Chris@303 37 setLayout(grid);
Chris@303 38
Chris@303 39 QGroupBox *databox = new QGroupBox(tr("Image"));
Chris@303 40
Chris@303 41 QGridLayout *subgrid = new QGridLayout;
Chris@303 42 databox->setLayout(subgrid);
Chris@303 43
Chris@303 44 int row = 0;
Chris@303 45
Chris@303 46 subgrid->addWidget(new QLabel(tr("Label:")), row, 0);
Chris@303 47
Chris@303 48 m_labelEdit = new QLineEdit;
Chris@303 49 subgrid->addWidget(m_labelEdit, row, 1, 1, 2);
Chris@303 50
Chris@303 51 ++row;
Chris@303 52
Chris@303 53 subgrid->addWidget(new QLabel(tr("File:")), row, 0);
Chris@303 54
Chris@303 55 m_imageEdit = new QLineEdit;
Chris@303 56 m_imageEdit->setReadOnly(true);
Chris@303 57 subgrid->addWidget(m_imageEdit, row, 1, 1, 1);
Chris@303 58
Chris@303 59 QPushButton *browse = new QPushButton(tr("Browse..."));
Chris@303 60 connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked()));
Chris@303 61 subgrid->addWidget(browse, row, 2, 1, 1);
Chris@303 62
Chris@303 63 ++row;
Chris@303 64
Chris@303 65 QGroupBox *previewbox = new QGroupBox(tr("Preview"));
Chris@303 66
Chris@303 67 subgrid = new QGridLayout;
Chris@303 68 previewbox->setLayout(subgrid);
Chris@303 69
Chris@303 70 m_imagePreview = new QLabel;
Chris@303 71 m_imagePreview->setAlignment(Qt::AlignCenter);
Chris@303 72 subgrid->addWidget(m_imagePreview, 0, 0);
Chris@303 73
Chris@303 74 m_imagePreview->setMinimumSize(QSize(100, 100));
Chris@303 75
Chris@303 76 grid->addWidget(databox, 0, 0);
Chris@303 77 grid->addWidget(previewbox, 1, 0);
Chris@303 78
Chris@303 79 grid->setRowStretch(1, 10);
Chris@303 80
Chris@303 81 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@303 82 QDialogButtonBox::Cancel);
Chris@303 83 grid->addWidget(bb, 2, 0, 1, 1);
Chris@303 84 connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@303 85 connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
Chris@303 86
Chris@303 87 m_okButton = bb->button(QDialogButtonBox::Ok);
Chris@303 88 m_okButton->setEnabled(false);
Chris@303 89
Chris@303 90 if (image != "") setImage(image);
Chris@303 91 if (label != "") setLabel(label);
Chris@303 92 }
Chris@303 93
Chris@303 94 ImageDialog::~ImageDialog()
Chris@303 95 {
Chris@303 96 }
Chris@303 97
Chris@303 98 QString
Chris@303 99 ImageDialog::getImage()
Chris@303 100 {
Chris@303 101 return m_loadedImageFile;
Chris@303 102 }
Chris@303 103
Chris@303 104 QPixmap
Chris@303 105 ImageDialog::getPixmap()
Chris@303 106 {
Chris@303 107 return m_loadedImage;
Chris@303 108 }
Chris@303 109
Chris@303 110 QString
Chris@303 111 ImageDialog::getLabel()
Chris@303 112 {
Chris@303 113 return m_labelEdit->text();
Chris@303 114 }
Chris@303 115
Chris@303 116 void
Chris@303 117 ImageDialog::setImage(QString image)
Chris@303 118 {
Chris@303 119 m_imageEdit->setText(image);
Chris@303 120 updatePreview();
Chris@303 121 }
Chris@303 122
Chris@303 123 void
Chris@303 124 ImageDialog::setLabel(QString label)
Chris@303 125 {
Chris@303 126 m_labelEdit->setText(label);
Chris@303 127 }
Chris@303 128
Chris@303 129 void
Chris@303 130 ImageDialog::resizeEvent(QResizeEvent *)
Chris@303 131 {
Chris@303 132 updatePreview();
Chris@303 133 }
Chris@303 134
Chris@303 135 void
Chris@303 136 ImageDialog::updatePreview()
Chris@303 137 {
Chris@303 138 if (!m_imagePreview) return;
Chris@303 139
Chris@303 140 QString img = m_imageEdit->text();
Chris@303 141
Chris@303 142 if (img != m_loadedImageFile) {
Chris@303 143 m_loadedImage = QPixmap(img);
Chris@303 144 m_loadedImageFile = img;
Chris@303 145 }
Chris@303 146
Chris@303 147 QSize sz(m_imagePreview->size());
Chris@303 148 int m = m_imagePreview->margin() * 2;
Chris@303 149 sz -= QSize(m, m);
Chris@303 150
Chris@303 151 if (m_loadedImage.isNull()) {
Chris@303 152 m_imagePreview->setPixmap(QPixmap());
Chris@303 153 m_okButton->setEnabled(false);
Chris@303 154 } else {
Chris@303 155 m_imagePreview->setPixmap(m_loadedImage.scaled
Chris@303 156 (sz,
Chris@303 157 Qt::KeepAspectRatio,
Chris@303 158 Qt::SmoothTransformation));
Chris@303 159 m_okButton->setEnabled(true);
Chris@303 160 }
Chris@303 161 }
Chris@303 162
Chris@303 163 void
Chris@303 164 ImageDialog::browseClicked()
Chris@303 165 {
Chris@303 166 QString file =
Chris@303 167 FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile);
Chris@303 168
Chris@303 169 if (file != "") {
Chris@303 170 setImage(file);
Chris@303 171 emit imageChanged(file);
Chris@303 172 }
Chris@303 173 }
Chris@303 174
Chris@303 175
Chris@303 176