Mercurial > hg > svgui
changeset 303:46faec7aae12
* Phase 1 of an image layer.
author | Chris Cannam |
---|---|
date | Thu, 04 Oct 2007 16:34:11 +0000 |
parents | e9549ea3f825 |
children | 4b7e8da8f069 |
files | layer/ImageLayer.cpp layer/ImageLayer.h layer/LayerFactory.cpp layer/LayerFactory.h layer/layer.pro widgets/ImageDialog.cpp widgets/ImageDialog.h widgets/widgets.pro |
diffstat | 8 files changed, 1106 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/ImageLayer.cpp Thu Oct 04 16:34:11 2007 +0000 @@ -0,0 +1,719 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006 Chris Cannam. + + 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. See the file + COPYING included with this distribution for more information. +*/ + +#include "ImageLayer.h" + +#include "data/model/Model.h" +#include "base/RealTime.h" +#include "base/Profiler.h" +#include "view/View.h" + +#include "data/model/ImageModel.h" + +#include "widgets/ImageDialog.h" + +#include <QPainter> +#include <QMouseEvent> +#include <QInputDialog> + +#include <iostream> +#include <cmath> + +ImageLayer::ImageMap +ImageLayer::m_images; + +ImageLayer::ImageLayer() : + Layer(), + m_model(0), + m_editing(false), + m_originalPoint(0, "", ""), + m_editingPoint(0, "", ""), + m_editingCommand(0) +{ + +} + +void +ImageLayer::setModel(ImageModel *model) +{ + if (m_model == model) return; + m_model = model; + + connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); + connect(m_model, SIGNAL(modelChanged(size_t, size_t)), + this, SIGNAL(modelChanged(size_t, size_t))); + + connect(m_model, SIGNAL(completionChanged()), + this, SIGNAL(modelCompletionChanged())); + +// std::cerr << "ImageLayer::setModel(" << model << ")" << std::endl; + + emit modelReplaced(); +} + +Layer::PropertyList +ImageLayer::getProperties() const +{ + return Layer::getProperties(); +} + +QString +ImageLayer::getPropertyLabel(const PropertyName &name) const +{ + return ""; +} + +Layer::PropertyType +ImageLayer::getPropertyType(const PropertyName &name) const +{ + return Layer::getPropertyType(name); +} + +int +ImageLayer::getPropertyRangeAndValue(const PropertyName &name, + int *min, int *max, int *deflt) const +{ + return Layer::getPropertyRangeAndValue(name, min, max, deflt); +} + +QString +ImageLayer::getPropertyValueLabel(const PropertyName &name, + int value) const +{ + return Layer::getPropertyValueLabel(name, value); +} + +void +ImageLayer::setProperty(const PropertyName &name, int value) +{ + Layer::setProperty(name, value); +} + +bool +ImageLayer::getValueExtents(float &, float &, bool &, QString &) const +{ + return false; +} + +bool +ImageLayer::isLayerScrollable(const View *v) const +{ + QPoint discard; + return !v->shouldIlluminateLocalFeatures(this, discard); +} + + +ImageModel::PointList +ImageLayer::getLocalPoints(View *v, int x, int y) const +{ + if (!m_model) return ImageModel::PointList(); + + std::cerr << "ImageLayer::getLocalPoints(" << x << "," << y << "):"; + + long frame0 = v->getFrameForX(-150); + long frame1 = v->getFrameForX(v->width() + 150); + + ImageModel::PointList points(m_model->getPoints(frame0, frame1)); + + ImageModel::PointList rv; + + //!!! need to store drawn size as well as original size for each + //image, but for now: + + for (ImageModel::PointList::iterator i = points.begin(); + i != points.end(); ++i) { + + const ImageModel::Point &p(*i); + + int px = v->getXForFrame(p.frame); + + if (x >= px && x < px + 100) { + rv.insert(p); + } + } + + std::cerr << rv.size() << " point(s)" << std::endl; + + return rv; +} + +QString +ImageLayer::getFeatureDescription(View *v, QPoint &pos) const +{ + int x = pos.x(); + + if (!m_model || !m_model->getSampleRate()) return ""; + + ImageModel::PointList points = getLocalPoints(v, x, pos.y()); + + if (points.empty()) { + if (!m_model->isReady()) { + return tr("In progress"); + } else { + return ""; + } + } + + long useFrame = points.begin()->frame; + + RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); + + QString text; +/* + if (points.begin()->label == "") { + text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3")) + .arg(rt.toText(true).c_str()) + .arg(points.begin()->height) + .arg(points.begin()->label); + } + + pos = QPoint(v->getXForFrame(useFrame), + getYForHeight(v, points.begin()->height)); +*/ + return text; +} + + +//!!! too much overlap with TimeValueLayer/TimeInstantLayer/TextLayer + +bool +ImageLayer::snapToFeatureFrame(View *v, int &frame, + size_t &resolution, + SnapType snap) const +{ + if (!m_model) { + return Layer::snapToFeatureFrame(v, frame, resolution, snap); + } + + resolution = m_model->getResolution(); + ImageModel::PointList points; + + if (snap == SnapNeighbouring) { + + points = getLocalPoints(v, v->getXForFrame(frame), -1); + if (points.empty()) return false; + frame = points.begin()->frame; + return true; + } + + points = m_model->getPoints(frame, frame); + int snapped = frame; + bool found = false; + + for (ImageModel::PointList::const_iterator i = points.begin(); + i != points.end(); ++i) { + + if (snap == SnapRight) { + + if (i->frame > frame) { + snapped = i->frame; + found = true; + break; + } + + } else if (snap == SnapLeft) { + + if (i->frame <= frame) { + snapped = i->frame; + found = true; // don't break, as the next may be better + } else { + break; + } + + } else { // nearest + + ImageModel::PointList::const_iterator j = i; + ++j; + + if (j == points.end()) { + + snapped = i->frame; + found = true; + break; + + } else if (j->frame >= frame) { + + if (j->frame - frame < frame - i->frame) { + snapped = j->frame; + } else { + snapped = i->frame; + } + found = true; + break; + } + } + } + + frame = snapped; + return found; +} + +void +ImageLayer::paint(View *v, QPainter &paint, QRect rect) const +{ + if (!m_model || !m_model->isOK()) return; + + int sampleRate = m_model->getSampleRate(); + if (!sampleRate) return; + +// Profiler profiler("ImageLayer::paint", true); + + int x0 = rect.left(), x1 = rect.right(); + long frame0 = v->getFrameForX(x0); + long frame1 = v->getFrameForX(x1); + + ImageModel::PointList points(m_model->getPoints(frame0, frame1)); + if (points.empty()) return; + + QColor penColour; + penColour = v->getForeground(); + +// std::cerr << "ImageLayer::paint: resolution is " +// << m_model->getResolution() << " frames" << std::endl; + + QPoint localPos; + long illuminateFrame = -1; + + if (v->shouldIlluminateLocalFeatures(this, localPos)) { + ImageModel::PointList localPoints = getLocalPoints(v, localPos.x(), + localPos.y()); + if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; + } + + paint.save(); + paint.setClipRect(rect.x(), 0, rect.width(), v->height()); + + for (ImageModel::PointList::const_iterator i = points.begin(); + i != points.end(); ++i) { + + const ImageModel::Point &p(*i); + + int x = v->getXForFrame(p.frame); + + int nx = x + 2000; + ImageModel::PointList::const_iterator j = i; + ++j; + if (j != points.end()) { + int jx = v->getXForFrame(j->frame); + if (jx < nx) nx = jx; + } +/* + if (illuminateFrame == p.frame) { + paint.setBrush(penColour); + paint.setPen(v->getBackground()); + } else { + paint.setPen(penColour); + paint.setBrush(brushColour); + } +*/ + QString label = p.label; + QString imageName = p.image; + + int nw = nx - x; + if (nw < 10) nw = 20; + + int top = 10; + if (v->height() < 50) top = 5; + + int bottom = top; + + QRect labelRect; + if (label != "") { + float aspect = getImageAspect(imageName); + int iw = lrintf((v->height() - v->height()/4 - top - bottom) + * aspect); + labelRect = paint.fontMetrics().boundingRect + (QRect(0, 0, iw, v->height()/4), + Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, label); + bottom += labelRect.height() + 5; + } + + QImage image = getImage(v, + imageName, + QSize(nw, v->height() - top - bottom)); + + if (image.isNull()) { + image = QImage(":icons/emptypage.png"); + } + + paint.setRenderHint(QPainter::Antialiasing, false); + + int boxWidth = image.width(); + + if (label != "") { + boxWidth = std::max(boxWidth, labelRect.width()); + } + + paint.drawRect(x-1, top-1, boxWidth+1, v->height() - 2 * top +1); + + paint.setRenderHint(QPainter::Antialiasing, true); + + paint.drawImage(x + (boxWidth - image.width())/2, top, image); + + paint.drawText(QRect(x, v->height() - bottom + 5, + boxWidth, labelRect.height()), + Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, + label); + } + + paint.restore(); + paint.setRenderHint(QPainter::Antialiasing, false); +} + +void +ImageLayer::setLayerDormant(const View *v, bool dormant) +{ + if (dormant) { + // Delete the images named in the view's scaled map from the + // general image map as well. They can always be re-loaded + // if it turns out another view still needs them. + for (ImageMap::iterator i = m_scaled[v].begin(); + i != m_scaled[v].end(); ++i) { + m_images.erase(i->first); + } + m_scaled.erase(v); + } +} + +//!!! how to reap no-longer-used images? + +float +ImageLayer::getImageAspect(QString name) const +{ + if (m_images.find(name) == m_images.end()) { + m_images[name] = QImage(name); + } + if (m_images[name].isNull()) return 1.f; + return float(m_images[name].width()) / float(m_images[name].height()); +} + +QImage +ImageLayer::getImage(View *v, QString name, QSize maxSize) const +{ + bool need = false; + +// std::cerr << "ImageLayer::getImage(" << v << ", " << name.toStdString() << ", (" +// << maxSize.width() << "x" << maxSize.height() << "))" << std::endl; + + if (!m_scaled[v][name].isNull() && + (m_scaled[v][name].width() == maxSize.width() || + m_scaled[v][name].height() == maxSize.height())) { +// std::cerr << "cache hit" << std::endl; + return m_scaled[v][name]; + } + + if (m_images.find(name) == m_images.end()) { + m_images[name] = QImage(name); + } + + if (m_images[name].isNull()) { +// std::cerr << "null image" << std::endl; + m_scaled[v][name] = QImage(); + } else { + m_scaled[v][name] = + m_images[name].scaled(maxSize, + Qt::KeepAspectRatio, + Qt::SmoothTransformation); + } + + return m_scaled[v][name]; +} + +void +ImageLayer::drawStart(View *v, QMouseEvent *e) +{ +// std::cerr << "ImageLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl; + + if (!m_model) { + std::cerr << "ImageLayer::drawStart: no model" << std::endl; + return; + } + + long frame = v->getFrameForX(e->x()); + if (frame < 0) frame = 0; + frame = frame / m_model->getResolution() * m_model->getResolution(); + + m_editingPoint = ImageModel::Point(frame, "", ""); + m_originalPoint = m_editingPoint; + + if (m_editingCommand) m_editingCommand->finish(); + m_editingCommand = new ImageModel::EditCommand(m_model, "Add Image"); + m_editingCommand->addPoint(m_editingPoint); + + m_editing = true; +} + +void +ImageLayer::drawDrag(View *v, QMouseEvent *e) +{ +// std::cerr << "ImageLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl; + + if (!m_model || !m_editing) return; + + long frame = v->getFrameForX(e->x()); + if (frame < 0) frame = 0; + frame = frame / m_model->getResolution() * m_model->getResolution(); + + m_editingCommand->deletePoint(m_editingPoint); + m_editingPoint.frame = frame; + m_editingCommand->addPoint(m_editingPoint); +} + +void +ImageLayer::drawEnd(View *v, QMouseEvent *) +{ +// std::cerr << "ImageLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; + if (!m_model || !m_editing) return; + + bool ok = false; + + ImageDialog dialog(tr("Select image"), "", tr("<no label>")); + if (dialog.exec() == QDialog::Accepted) { + ImageModel::ChangeImageCommand *command = + new ImageModel::ChangeImageCommand + (m_model, m_editingPoint, dialog.getImage(), dialog.getLabel()); + m_editingCommand->addCommand(command); + } + + m_editingCommand->finish(); + m_editingCommand = 0; + m_editing = false; +} + +void +ImageLayer::editStart(View *v, QMouseEvent *e) +{ +// std::cerr << "ImageLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; + + if (!m_model) return; + + ImageModel::PointList points = getLocalPoints(v, e->x(), e->y()); + if (points.empty()) return; + + m_editOrigin = e->pos(); + m_editingPoint = *points.begin(); + m_originalPoint = m_editingPoint; + + if (m_editingCommand) { + m_editingCommand->finish(); + m_editingCommand = 0; + } + + m_editing = true; +} + +void +ImageLayer::editDrag(View *v, QMouseEvent *e) +{ + if (!m_model || !m_editing) return; + + long frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x()); + long frame = m_originalPoint.frame + frameDiff; + + if (frame < 0) frame = 0; + frame = (frame / m_model->getResolution()) * m_model->getResolution(); + + if (!m_editingCommand) { + m_editingCommand = new ImageModel::EditCommand(m_model, tr("Move Image")); + } + + m_editingCommand->deletePoint(m_editingPoint); + m_editingPoint.frame = frame; + m_editingCommand->addPoint(m_editingPoint); +} + +void +ImageLayer::editEnd(View *, QMouseEvent *) +{ +// std::cerr << "ImageLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; + if (!m_model || !m_editing) return; + + if (m_editingCommand) { + m_editingCommand->finish(); + } + + m_editingCommand = 0; + m_editing = false; +} + +bool +ImageLayer::editOpen(View *v, QMouseEvent *e) +{ + if (!m_model) return false; + + ImageModel::PointList points = getLocalPoints(v, e->x(), e->y()); + if (points.empty()) return false; + + QString image = points.begin()->image; + QString label = points.begin()->label; + + ImageDialog dialog(tr("Select image"), + image, + label); + + if (dialog.exec() == QDialog::Accepted) { + ImageModel::ChangeImageCommand *command = + new ImageModel::ChangeImageCommand + (m_model, *points.begin(), dialog.getImage(), dialog.getLabel()); + CommandHistory::getInstance()->addCommand(command); + } + + return true; +} + +void +ImageLayer::moveSelection(Selection s, size_t newStartFrame) +{ + if (!m_model) return; + + ImageModel::EditCommand *command = + new ImageModel::EditCommand(m_model, tr("Drag Selection")); + + ImageModel::PointList points = + m_model->getPoints(s.getStartFrame(), s.getEndFrame()); + + for (ImageModel::PointList::iterator i = points.begin(); + i != points.end(); ++i) { + + if (s.contains(i->frame)) { + ImageModel::Point newPoint(*i); + newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); + command->deletePoint(*i); + command->addPoint(newPoint); + } + } + + command->finish(); +} + +void +ImageLayer::resizeSelection(Selection s, Selection newSize) +{ + if (!m_model) return; + + ImageModel::EditCommand *command = + new ImageModel::EditCommand(m_model, tr("Resize Selection")); + + ImageModel::PointList points = + m_model->getPoints(s.getStartFrame(), s.getEndFrame()); + + double ratio = + double(newSize.getEndFrame() - newSize.getStartFrame()) / + double(s.getEndFrame() - s.getStartFrame()); + + for (ImageModel::PointList::iterator i = points.begin(); + i != points.end(); ++i) { + + if (s.contains(i->frame)) { + + double target = i->frame; + target = newSize.getStartFrame() + + double(target - s.getStartFrame()) * ratio; + + ImageModel::Point newPoint(*i); + newPoint.frame = lrint(target); + command->deletePoint(*i); + command->addPoint(newPoint); + } + } + + command->finish(); +} + +void +ImageLayer::deleteSelection(Selection s) +{ + if (!m_model) return; + + ImageModel::EditCommand *command = + new ImageModel::EditCommand(m_model, tr("Delete Selection")); + + ImageModel::PointList points = + m_model->getPoints(s.getStartFrame(), s.getEndFrame()); + + for (ImageModel::PointList::iterator i = points.begin(); + i != points.end(); ++i) { + if (s.contains(i->frame)) command->deletePoint(*i); + } + + command->finish(); +} + +void +ImageLayer::copy(Selection s, Clipboard &to) +{ + if (!m_model) return; + + ImageModel::PointList points = + m_model->getPoints(s.getStartFrame(), s.getEndFrame()); + + for (ImageModel::PointList::iterator i = points.begin(); + i != points.end(); ++i) { + if (s.contains(i->frame)) { + //!!! inadequate + Clipboard::Point point(i->frame, i->label); + to.addPoint(point); + } + } +} + +bool +ImageLayer::paste(const Clipboard &from, int frameOffset, bool /* interactive */) +{ + if (!m_model) return false; + + const Clipboard::PointList &points = from.getPoints(); + + ImageModel::EditCommand *command = + new ImageModel::EditCommand(m_model, tr("Paste")); + + for (Clipboard::PointList::const_iterator i = points.begin(); + i != points.end(); ++i) { + + if (!i->haveFrame()) continue; + size_t frame = 0; + if (frameOffset > 0 || -frameOffset < i->getFrame()) { + frame = i->getFrame() + frameOffset; + } + ImageModel::Point newPoint(frame); + + //!!! inadequate + + if (i->haveLabel()) { + newPoint.label = i->getLabel(); + } else if (i->haveValue()) { + newPoint.label = QString("%1").arg(i->getValue()); + } else { + newPoint.label = tr("New Point"); + } + + command->addPoint(newPoint); + } + + command->finish(); + return true; +} + +QString +ImageLayer::toXmlString(QString indent, QString extraAttributes) const +{ + return Layer::toXmlString(indent, extraAttributes); +} + +void +ImageLayer::setProperties(const QXmlAttributes &attributes) +{ +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/ImageLayer.h Thu Oct 04 16:34:11 2007 +0000 @@ -0,0 +1,119 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2007 Chris Cannam and QMUL. + + 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. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef _IMAGE_LAYER_H_ +#define _IMAGE_LAYER_H_ + +#include "Layer.h" +#include "data/model/ImageModel.h" + +#include <QObject> +#include <QColor> +#include <QImage> + +#include <map> + +class View; +class QPainter; + +class ImageLayer : public Layer +{ + Q_OBJECT + +public: + ImageLayer(); + + virtual void paint(View *v, QPainter &paint, QRect rect) const; + + virtual QString getFeatureDescription(View *v, QPoint &) const; + + virtual bool snapToFeatureFrame(View *v, int &frame, + size_t &resolution, + SnapType snap) const; + + virtual void drawStart(View *v, QMouseEvent *); + virtual void drawDrag(View *v, QMouseEvent *); + virtual void drawEnd(View *v, QMouseEvent *); + + virtual void editStart(View *v, QMouseEvent *); + virtual void editDrag(View *v, QMouseEvent *); + virtual void editEnd(View *v, QMouseEvent *); + + virtual void moveSelection(Selection s, size_t newStartFrame); + virtual void resizeSelection(Selection s, Selection newSize); + virtual void deleteSelection(Selection s); + + virtual void copy(Selection s, Clipboard &to); + virtual bool paste(const Clipboard &from, int frameOffset, + bool interactive); + + virtual bool editOpen(View *, QMouseEvent *); // on double-click + + virtual const Model *getModel() const { return m_model; } + void setModel(ImageModel *model); + + virtual PropertyList getProperties() const; + virtual QString getPropertyLabel(const PropertyName &) const; + virtual PropertyType getPropertyType(const PropertyName &) const; + virtual int getPropertyRangeAndValue(const PropertyName &, + int *min, int *max, int *deflt) const; + virtual QString getPropertyValueLabel(const PropertyName &, + int value) const; + virtual void setProperty(const PropertyName &, int value); + + virtual ColourSignificance getLayerColourSignificance() const { + return ColourAbsent; + } + + virtual bool isLayerScrollable(const View *v) const; + + virtual bool isLayerEditable() const { return true; } + + virtual int getCompletion(View *) const { return m_model->getCompletion(); } + + virtual bool getValueExtents(float &min, float &max, + bool &logarithmic, QString &unit) const; + + virtual QString toXmlString(QString indent = "", + QString extraAttributes = "") const; + + virtual void setLayerDormant(const View *v, bool dormant); + + void setProperties(const QXmlAttributes &attributes); + +protected: + ImageModel::PointList getLocalPoints(View *v, int x, int y) const; + + float getImageAspect(QString name) const; + QImage getImage(View *v, QString name, QSize maxSize) const; + + //!!! how to reap no-longer-used images? + + typedef std::map<QString, QImage> ImageMap; + typedef std::map<const View *, ImageMap> ViewImageMap; + + static ImageMap m_images; + mutable ViewImageMap m_scaled; + + ImageModel *m_model; + bool m_editing; + QPoint m_editOrigin; + ImageModel::Point m_originalPoint; + ImageModel::Point m_editingPoint; + ImageModel::EditCommand *m_editingCommand; +}; + +#endif +
--- a/layer/LayerFactory.cpp Mon Oct 01 13:48:38 2007 +0000 +++ b/layer/LayerFactory.cpp Thu Oct 04 16:34:11 2007 +0000 @@ -22,6 +22,7 @@ #include "TimeValueLayer.h" #include "NoteLayer.h" #include "TextLayer.h" +#include "ImageLayer.h" #include "Colour3DPlotLayer.h" #include "SpectrumLayer.h" #include "SliceLayer.h" @@ -33,6 +34,7 @@ #include "data/model/SparseTimeValueModel.h" #include "data/model/NoteModel.h" #include "data/model/TextModel.h" +#include "data/model/ImageModel.h" #include "data/model/DenseThreeDimensionalModel.h" #include "data/model/WaveFileModel.h" #include "data/model/WritableWaveFileModel.h" @@ -61,6 +63,7 @@ case TimeValues: return Layer::tr("Time Values"); case Notes: return Layer::tr("Notes"); case Text: return Layer::tr("Text"); + case Image: return Layer::tr("Images"); case Colour3DPlot: return Layer::tr("Colour 3D Plot"); case Spectrum: return Layer::tr("Spectrum"); case Slice: return Layer::tr("Time Slice"); @@ -150,6 +153,10 @@ types.insert(Text); } + if (dynamic_cast<ImageModel *>(model)) { + types.insert(Image); + } + if (dynamic_cast<DenseTimeValueModel *>(model)) { types.insert(Spectrum); } @@ -168,6 +175,7 @@ types.insert(TimeValues); types.insert(Notes); types.insert(Text); + types.insert(Image); //!!! and in principle Colour3DPlot -- now that's a challenge return types; } @@ -182,6 +190,7 @@ if (dynamic_cast<const TimeValueLayer *>(layer)) return TimeValues; if (dynamic_cast<const NoteLayer *>(layer)) return Notes; if (dynamic_cast<const TextLayer *>(layer)) return Text; + if (dynamic_cast<const ImageLayer *>(layer)) return Image; if (dynamic_cast<const Colour3DPlotLayer *>(layer)) return Colour3DPlot; if (dynamic_cast<const SpectrumLayer *>(layer)) return Spectrum; if (dynamic_cast<const SliceLayer *>(layer)) return Slice; @@ -199,6 +208,7 @@ case TimeValues: return "values"; case Notes: return "notes"; case Text: return "text"; + case Image: return "image"; case Colour3DPlot: return "colour3d"; case Spectrum: return "spectrum"; case Slice: return "spectrum"; @@ -217,6 +227,7 @@ case TimeValues: return "timevalues"; case Notes: return "notes"; case Text: return "text"; + case Image: return "image"; case Colour3DPlot: return "colour3dplot"; case Spectrum: return "spectrum"; case Slice: return "slice"; @@ -234,6 +245,7 @@ if (name == "timevalues") return TimeValues; if (name == "notes") return Notes; if (name == "text") return Text; + if (name == "image") return Image; if (name == "colour3dplot") return Colour3DPlot; if (name == "spectrum") return Spectrum; if (name == "slice") return Slice; @@ -270,6 +282,9 @@ if (trySetModel<TextLayer, TextModel>(layer, model)) return; + if (trySetModel<ImageLayer, ImageModel>(layer, model)) + return; + if (trySetModel<Colour3DPlotLayer, DenseThreeDimensionalModel>(layer, model)) return; @@ -294,6 +309,8 @@ return new NoteModel(baseModel->getSampleRate(), 1, true); } else if (layerType == Text) { return new TextModel(baseModel->getSampleRate(), 1, true); + } else if (layerType == Image) { + return new ImageModel(baseModel->getSampleRate(), 1, true); } else { return 0; } @@ -359,6 +376,10 @@ layer = new TextLayer; break; + case Image: + layer = new ImageLayer; + break; + case Colour3DPlot: layer = new Colour3DPlotLayer; break;
--- a/layer/LayerFactory.h Mon Oct 01 13:48:38 2007 +0000 +++ b/layer/LayerFactory.h Thu Oct 04 16:34:11 2007 +0000 @@ -35,6 +35,7 @@ TimeValues, Notes, Text, + Image, Colour3DPlot, Spectrum, Slice,
--- a/layer/layer.pro Mon Oct 01 13:48:38 2007 +0000 +++ b/layer/layer.pro Thu Oct 04 16:34:11 2007 +0000 @@ -15,6 +15,7 @@ # Input HEADERS += Colour3DPlotLayer.h \ + ImageLayer.h \ ImageRegionFinder.h \ Layer.h \ LayerFactory.h \ @@ -31,6 +32,7 @@ TimeValueLayer.h \ WaveformLayer.h SOURCES += Colour3DPlotLayer.cpp \ + ImageLayer.cpp \ ImageRegionFinder.cpp \ Layer.cpp \ LayerFactory.cpp \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/widgets/ImageDialog.cpp Thu Oct 04 16:34:11 2007 +0000 @@ -0,0 +1,176 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2007 QMUL. + + 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. See the file + COPYING included with this distribution for more information. +*/ + +#include "ImageDialog.h" + +#include <QLineEdit> +#include <QGridLayout> +#include <QLabel> +#include <QDialogButtonBox> +#include <QPushButton> +#include <QGroupBox> + +#include "data/fileio/FileFinder.h" + +ImageDialog::ImageDialog(QString title, + QString image, + QString label, + QWidget *parent) : + QDialog(parent), + m_imagePreview(0) +{ + setWindowTitle(title); + + QGridLayout *grid = new QGridLayout; + setLayout(grid); + + QGroupBox *databox = new QGroupBox(tr("Image")); + + QGridLayout *subgrid = new QGridLayout; + databox->setLayout(subgrid); + + int row = 0; + + subgrid->addWidget(new QLabel(tr("Label:")), row, 0); + + m_labelEdit = new QLineEdit; + subgrid->addWidget(m_labelEdit, row, 1, 1, 2); + + ++row; + + subgrid->addWidget(new QLabel(tr("File:")), row, 0); + + m_imageEdit = new QLineEdit; + m_imageEdit->setReadOnly(true); + subgrid->addWidget(m_imageEdit, row, 1, 1, 1); + + QPushButton *browse = new QPushButton(tr("Browse...")); + connect(browse, SIGNAL(clicked()), this, SLOT(browseClicked())); + subgrid->addWidget(browse, row, 2, 1, 1); + + ++row; + + QGroupBox *previewbox = new QGroupBox(tr("Preview")); + + subgrid = new QGridLayout; + previewbox->setLayout(subgrid); + + m_imagePreview = new QLabel; + m_imagePreview->setAlignment(Qt::AlignCenter); + subgrid->addWidget(m_imagePreview, 0, 0); + + m_imagePreview->setMinimumSize(QSize(100, 100)); + + grid->addWidget(databox, 0, 0); + grid->addWidget(previewbox, 1, 0); + + grid->setRowStretch(1, 10); + + QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok | + QDialogButtonBox::Cancel); + grid->addWidget(bb, 2, 0, 1, 1); + connect(bb, SIGNAL(accepted()), this, SLOT(accept())); + connect(bb, SIGNAL(rejected()), this, SLOT(reject())); + + m_okButton = bb->button(QDialogButtonBox::Ok); + m_okButton->setEnabled(false); + + if (image != "") setImage(image); + if (label != "") setLabel(label); +} + +ImageDialog::~ImageDialog() +{ +} + +QString +ImageDialog::getImage() +{ + return m_loadedImageFile; +} + +QPixmap +ImageDialog::getPixmap() +{ + return m_loadedImage; +} + +QString +ImageDialog::getLabel() +{ + return m_labelEdit->text(); +} + +void +ImageDialog::setImage(QString image) +{ + m_imageEdit->setText(image); + updatePreview(); +} + +void +ImageDialog::setLabel(QString label) +{ + m_labelEdit->setText(label); +} + +void +ImageDialog::resizeEvent(QResizeEvent *) +{ + updatePreview(); +} + +void +ImageDialog::updatePreview() +{ + if (!m_imagePreview) return; + + QString img = m_imageEdit->text(); + + if (img != m_loadedImageFile) { + m_loadedImage = QPixmap(img); + m_loadedImageFile = img; + } + + QSize sz(m_imagePreview->size()); + int m = m_imagePreview->margin() * 2; + sz -= QSize(m, m); + + if (m_loadedImage.isNull()) { + m_imagePreview->setPixmap(QPixmap()); + m_okButton->setEnabled(false); + } else { + m_imagePreview->setPixmap(m_loadedImage.scaled + (sz, + Qt::KeepAspectRatio, + Qt::SmoothTransformation)); + m_okButton->setEnabled(true); + } +} + +void +ImageDialog::browseClicked() +{ + QString file = + FileFinder::getInstance()->getOpenFileName(FileFinder::ImageFile); + + if (file != "") { + setImage(file); + emit imageChanged(file); + } +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/widgets/ImageDialog.h Thu Oct 04 16:34:11 2007 +0000 @@ -0,0 +1,66 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2007 QMUL. + + 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. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef _IMAGE_DIALOG_H_ +#define _IMAGE_DIALOG_H_ + +#include <QDialog> +#include <QString> + +class QLineEdit; +class QLabel; +class QPushButton; + +class ImageDialog : public QDialog +{ + Q_OBJECT + +public: + ImageDialog(QString title, + QString image = "", + QString label = "", + QWidget *parent = 0); + virtual ~ImageDialog(); + + QString getImage(); + QPixmap getPixmap(); + QString getLabel(); + +signals: + void imageChanged(QString image); + void labelChanged(QString label); + +public slots: + void setImage(QString image); + void setLabel(QString label); + void updatePreview(); + +protected slots: + void browseClicked(); + +protected: + void resizeEvent(QResizeEvent *); + + QLineEdit *m_imageEdit; + QLineEdit *m_labelEdit; + QLabel *m_imagePreview; + + QString m_loadedImageFile; + QPixmap m_loadedImage; + + QPushButton *m_okButton; +}; + +#endif
--- a/widgets/widgets.pro Mon Oct 01 13:48:38 2007 +0000 +++ b/widgets/widgets.pro Thu Oct 04 16:34:11 2007 +0000 @@ -18,6 +18,7 @@ ColourNameDialog.h \ Fader.h \ IconLoader.h \ + ImageDialog.h \ ItemEditDialog.h \ KeyReference.h \ LayerTree.h \ @@ -42,6 +43,7 @@ ColourNameDialog.cpp \ Fader.cpp \ IconLoader.cpp \ + ImageDialog.cpp \ ItemEditDialog.cpp \ KeyReference.cpp \ LayerTree.cpp \