Chris@303: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@303: Chris@303: /* Chris@303: Sonic Visualiser Chris@303: An audio file viewer and annotation editor. Chris@303: Centre for Digital Music, Queen Mary, University of London. Chris@303: This file copyright 2006 Chris Cannam. Chris@303: Chris@303: This program is free software; you can redistribute it and/or Chris@303: modify it under the terms of the GNU General Public License as Chris@303: published by the Free Software Foundation; either version 2 of the Chris@303: License, or (at your option) any later version. See the file Chris@303: COPYING included with this distribution for more information. Chris@303: */ Chris@303: Chris@303: #include "ImageLayer.h" Chris@303: Chris@303: #include "data/model/Model.h" Chris@303: #include "base/RealTime.h" Chris@303: #include "base/Profiler.h" Chris@303: #include "view/View.h" Chris@303: Chris@303: #include "data/model/ImageModel.h" Chris@318: #include "data/fileio/FileSource.h" Chris@303: Chris@303: #include "widgets/ImageDialog.h" Chris@303: Chris@303: #include Chris@303: #include Chris@303: #include Chris@305: #include Chris@316: #include Chris@303: Chris@303: #include Chris@303: #include Chris@303: Chris@303: ImageLayer::ImageMap Chris@303: ImageLayer::m_images; Chris@303: Chris@305: QMutex Chris@305: ImageLayer::m_imageMapMutex; Chris@305: Chris@303: ImageLayer::ImageLayer() : Chris@303: Layer(), Chris@303: m_model(0), Chris@303: m_editing(false), Chris@303: m_originalPoint(0, "", ""), Chris@303: m_editingPoint(0, "", ""), Chris@303: m_editingCommand(0) Chris@303: { Chris@305: } Chris@305: Chris@305: ImageLayer::~ImageLayer() Chris@305: { Chris@318: for (FileSourceMap::iterator i = m_remoteFiles.begin(); Chris@305: i != m_remoteFiles.end(); ++i) { Chris@305: delete i->second; Chris@305: } Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::setModel(ImageModel *model) Chris@303: { Chris@303: if (m_model == model) return; Chris@303: m_model = model; Chris@303: Chris@305: connect(m_model, SIGNAL(modelChanged()), Chris@305: this, SIGNAL(modelChanged())); Chris@303: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@303: this, SIGNAL(modelChanged(size_t, size_t))); Chris@303: Chris@303: connect(m_model, SIGNAL(completionChanged()), Chris@303: this, SIGNAL(modelCompletionChanged())); Chris@303: Chris@305: // connect(m_model, SIGNAL(modelChanged()), Chris@305: // this, SLOT(checkAddRemotes())); Chris@305: Chris@303: // std::cerr << "ImageLayer::setModel(" << model << ")" << std::endl; Chris@303: Chris@305: // checkAddRemotes(); Chris@305: Chris@303: emit modelReplaced(); Chris@303: } Chris@303: Chris@303: Layer::PropertyList Chris@303: ImageLayer::getProperties() const Chris@303: { Chris@303: return Layer::getProperties(); Chris@303: } Chris@303: Chris@303: QString Chris@303: ImageLayer::getPropertyLabel(const PropertyName &name) const Chris@303: { Chris@303: return ""; Chris@303: } Chris@303: Chris@303: Layer::PropertyType Chris@303: ImageLayer::getPropertyType(const PropertyName &name) const Chris@303: { Chris@303: return Layer::getPropertyType(name); Chris@303: } Chris@303: Chris@303: int Chris@303: ImageLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@303: int *min, int *max, int *deflt) const Chris@303: { Chris@303: return Layer::getPropertyRangeAndValue(name, min, max, deflt); Chris@303: } Chris@303: Chris@303: QString Chris@303: ImageLayer::getPropertyValueLabel(const PropertyName &name, Chris@303: int value) const Chris@303: { Chris@303: return Layer::getPropertyValueLabel(name, value); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::setProperty(const PropertyName &name, int value) Chris@303: { Chris@303: Layer::setProperty(name, value); Chris@303: } Chris@303: Chris@303: bool Chris@303: ImageLayer::getValueExtents(float &, float &, bool &, QString &) const Chris@303: { Chris@303: return false; Chris@303: } Chris@303: Chris@303: bool Chris@303: ImageLayer::isLayerScrollable(const View *v) const Chris@303: { Chris@304: return true; Chris@303: } Chris@303: Chris@303: Chris@303: ImageModel::PointList Chris@303: ImageLayer::getLocalPoints(View *v, int x, int y) const Chris@303: { Chris@303: if (!m_model) return ImageModel::PointList(); Chris@303: Chris@304: // std::cerr << "ImageLayer::getLocalPoints(" << x << "," << y << "):"; Chris@304: const ImageModel::PointList &points(m_model->getPoints()); Chris@303: Chris@303: ImageModel::PointList rv; Chris@303: Chris@304: for (ImageModel::PointList::const_iterator i = points.begin(); Chris@304: i != points.end(); ) { Chris@303: Chris@303: const ImageModel::Point &p(*i); Chris@304: int px = v->getXForFrame(p.frame); Chris@304: if (px > x) break; Chris@303: Chris@304: ++i; Chris@304: if (i != points.end()) { Chris@304: int nx = v->getXForFrame((*i).frame); Chris@304: if (nx < x) { Chris@304: // as we aim not to overlap the images, if the following Chris@304: // image begins to the left of a point then the current Chris@304: // one may be assumed to end to the left of it as well. Chris@304: continue; Chris@304: } Chris@304: } Chris@303: Chris@304: // this image is a candidate, test it properly Chris@304: Chris@304: int width = 32; Chris@304: if (m_scaled[v].find(p.image) != m_scaled[v].end()) { Chris@304: width = m_scaled[v][p.image].width(); Chris@305: // std::cerr << "scaled width = " << width << std::endl; Chris@304: } Chris@304: Chris@304: if (x >= px && x < px + width) { Chris@303: rv.insert(p); Chris@303: } Chris@303: } Chris@303: Chris@304: // std::cerr << rv.size() << " point(s)" << std::endl; Chris@303: Chris@303: return rv; Chris@303: } Chris@303: Chris@303: QString Chris@303: ImageLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@303: { Chris@303: int x = pos.x(); Chris@303: Chris@303: if (!m_model || !m_model->getSampleRate()) return ""; Chris@303: Chris@303: ImageModel::PointList points = getLocalPoints(v, x, pos.y()); Chris@303: Chris@303: if (points.empty()) { Chris@303: if (!m_model->isReady()) { Chris@303: return tr("In progress"); Chris@303: } else { Chris@303: return ""; Chris@303: } Chris@303: } Chris@303: Chris@303: long useFrame = points.begin()->frame; Chris@303: Chris@303: RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); Chris@303: Chris@303: QString text; Chris@303: /* Chris@303: if (points.begin()->label == "") { Chris@303: text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3")) Chris@303: .arg(rt.toText(true).c_str()) Chris@303: .arg(points.begin()->height) Chris@303: .arg(points.begin()->label); Chris@303: } Chris@303: Chris@303: pos = QPoint(v->getXForFrame(useFrame), Chris@303: getYForHeight(v, points.begin()->height)); Chris@303: */ Chris@303: return text; Chris@303: } Chris@303: Chris@303: Chris@303: //!!! too much overlap with TimeValueLayer/TimeInstantLayer/TextLayer Chris@303: Chris@303: bool Chris@303: ImageLayer::snapToFeatureFrame(View *v, int &frame, Chris@303: size_t &resolution, Chris@303: SnapType snap) const Chris@303: { Chris@303: if (!m_model) { Chris@303: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@303: } Chris@303: Chris@303: resolution = m_model->getResolution(); Chris@303: ImageModel::PointList points; Chris@303: Chris@303: if (snap == SnapNeighbouring) { Chris@303: Chris@303: points = getLocalPoints(v, v->getXForFrame(frame), -1); Chris@303: if (points.empty()) return false; Chris@303: frame = points.begin()->frame; Chris@303: return true; Chris@303: } Chris@303: Chris@303: points = m_model->getPoints(frame, frame); Chris@303: int snapped = frame; Chris@303: bool found = false; Chris@303: Chris@303: for (ImageModel::PointList::const_iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: Chris@303: if (snap == SnapRight) { Chris@303: Chris@303: if (i->frame > frame) { Chris@303: snapped = i->frame; Chris@303: found = true; Chris@303: break; Chris@303: } Chris@303: Chris@303: } else if (snap == SnapLeft) { Chris@303: Chris@303: if (i->frame <= frame) { Chris@303: snapped = i->frame; Chris@303: found = true; // don't break, as the next may be better Chris@303: } else { Chris@303: break; Chris@303: } Chris@303: Chris@303: } else { // nearest Chris@303: Chris@303: ImageModel::PointList::const_iterator j = i; Chris@303: ++j; Chris@303: Chris@303: if (j == points.end()) { Chris@303: Chris@303: snapped = i->frame; Chris@303: found = true; Chris@303: break; Chris@303: Chris@303: } else if (j->frame >= frame) { Chris@303: Chris@303: if (j->frame - frame < frame - i->frame) { Chris@303: snapped = j->frame; Chris@303: } else { Chris@303: snapped = i->frame; Chris@303: } Chris@303: found = true; Chris@303: break; Chris@303: } Chris@303: } Chris@303: } Chris@303: Chris@303: frame = snapped; Chris@303: return found; Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@303: { Chris@303: if (!m_model || !m_model->isOK()) return; Chris@303: Chris@303: int sampleRate = m_model->getSampleRate(); Chris@303: if (!sampleRate) return; Chris@303: Chris@303: // Profiler profiler("ImageLayer::paint", true); Chris@303: Chris@304: // int x0 = rect.left(), x1 = rect.right(); Chris@304: int x0 = 0, x1 = v->width(); Chris@304: Chris@303: long frame0 = v->getFrameForX(x0); Chris@303: long frame1 = v->getFrameForX(x1); Chris@303: Chris@303: ImageModel::PointList points(m_model->getPoints(frame0, frame1)); Chris@303: if (points.empty()) return; Chris@303: Chris@304: paint.save(); Chris@304: paint.setClipRect(rect.x(), 0, rect.width(), v->height()); Chris@304: Chris@303: QColor penColour; Chris@303: penColour = v->getForeground(); Chris@303: Chris@304: QColor brushColour; Chris@304: brushColour = v->getBackground(); Chris@303: Chris@304: int h, s, val; Chris@304: brushColour.getHsv(&h, &s, &val); Chris@304: brushColour.setHsv(h, s, 255, 240); Chris@303: Chris@304: paint.setPen(penColour); Chris@304: paint.setBrush(brushColour); Chris@304: paint.setRenderHint(QPainter::Antialiasing, true); Chris@303: Chris@303: for (ImageModel::PointList::const_iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: Chris@303: const ImageModel::Point &p(*i); Chris@303: Chris@303: int x = v->getXForFrame(p.frame); Chris@303: Chris@303: int nx = x + 2000; Chris@303: ImageModel::PointList::const_iterator j = i; Chris@303: ++j; Chris@303: if (j != points.end()) { Chris@303: int jx = v->getXForFrame(j->frame); Chris@303: if (jx < nx) nx = jx; Chris@303: } Chris@303: Chris@304: drawImage(v, paint, p, x, nx); Chris@304: } Chris@303: Chris@304: paint.setRenderHint(QPainter::Antialiasing, false); Chris@304: paint.restore(); Chris@304: } Chris@303: Chris@304: void Chris@304: ImageLayer::drawImage(View *v, QPainter &paint, const ImageModel::Point &p, Chris@304: int x, int nx) const Chris@304: { Chris@304: QString label = p.label; Chris@304: QString imageName = p.image; Chris@304: Chris@304: QImage image; Chris@304: QString additionalText; Chris@304: Chris@304: QSize imageSize; Chris@304: if (!getImageOriginalSize(imageName, imageSize)) { Chris@304: image = QImage(":icons/emptypage.png"); Chris@304: imageSize = image.size(); Chris@304: additionalText = imageName; Chris@304: } Chris@304: Chris@304: int topMargin = 10; Chris@304: int bottomMargin = 10; Chris@304: int spacing = 5; Chris@304: Chris@304: if (v->height() < 100) { Chris@304: topMargin = 5; Chris@304: bottomMargin = 5; Chris@304: } Chris@304: Chris@304: int maxBoxHeight = v->height() - topMargin - bottomMargin; Chris@304: Chris@304: int availableWidth = nx - x - 3; Chris@304: if (availableWidth < 20) availableWidth = 20; Chris@304: Chris@304: QRect labelRect; Chris@304: Chris@304: if (label != "") { Chris@304: Chris@304: int likelyHeight = v->height() / 4; Chris@304: Chris@304: int likelyWidth = // available height times image aspect Chris@304: ((maxBoxHeight - likelyHeight) * imageSize.width()) Chris@304: / imageSize.height(); Chris@304: Chris@304: if (likelyWidth > imageSize.width()) { Chris@304: likelyWidth = imageSize.width(); Chris@303: } Chris@303: Chris@304: if (likelyWidth > availableWidth) { Chris@304: likelyWidth = availableWidth; Chris@303: } Chris@303: Chris@304: int singleWidth = paint.fontMetrics().width(label); Chris@304: if (singleWidth < availableWidth && singleWidth < likelyWidth * 2) { Chris@304: likelyWidth = singleWidth + 4; Chris@303: } Chris@303: Chris@304: labelRect = paint.fontMetrics().boundingRect Chris@304: (QRect(0, 0, likelyWidth, likelyHeight), Chris@304: Qt::AlignCenter | Qt::TextWordWrap, label); Chris@303: Chris@304: labelRect.setWidth(labelRect.width() + 6); Chris@303: } Chris@303: Chris@304: if (image.isNull()) { Chris@304: image = getImage(v, imageName, Chris@304: QSize(availableWidth, Chris@304: maxBoxHeight - labelRect.height())); Chris@304: } Chris@304: Chris@304: int boxWidth = image.width(); Chris@304: if (boxWidth < labelRect.width()) { Chris@304: boxWidth = labelRect.width(); Chris@304: } Chris@304: Chris@304: int boxHeight = image.height(); Chris@304: if (label != "") { Chris@304: boxHeight += labelRect.height() + spacing; Chris@304: } Chris@304: Chris@304: int division = image.height(); Chris@304: Chris@304: if (additionalText != "") { Chris@304: Chris@304: paint.save(); Chris@304: Chris@304: QFont font(paint.font()); Chris@304: font.setItalic(true); Chris@304: paint.setFont(font); Chris@304: Chris@304: int tw = paint.fontMetrics().width(additionalText); Chris@304: if (tw > availableWidth) { Chris@304: tw = availableWidth; Chris@304: } Chris@304: if (boxWidth < tw) { Chris@304: boxWidth = tw; Chris@304: } Chris@304: boxHeight += paint.fontMetrics().height(); Chris@304: division += paint.fontMetrics().height(); Chris@304: } Chris@304: Chris@304: bottomMargin = v->height() - topMargin - boxHeight; Chris@304: if (bottomMargin > topMargin + v->height()/7) { Chris@304: topMargin += v->height()/8; Chris@304: bottomMargin -= v->height()/8; Chris@304: } Chris@304: Chris@304: paint.drawRect(x - 1, Chris@304: topMargin - 1, Chris@304: boxWidth + 2, Chris@304: boxHeight + 2); Chris@304: Chris@304: int imageY; Chris@304: if (label != "") { Chris@304: imageY = topMargin + labelRect.height() + spacing; Chris@304: } else { Chris@304: imageY = topMargin; Chris@304: } Chris@304: Chris@304: paint.drawImage(x + (boxWidth - image.width())/2, Chris@304: imageY, Chris@304: image); Chris@304: Chris@304: if (additionalText != "") { Chris@304: paint.drawText(x, Chris@304: imageY + image.height() + paint.fontMetrics().ascent(), Chris@304: additionalText); Chris@304: paint.restore(); Chris@304: } Chris@304: Chris@304: if (label != "") { Chris@304: paint.drawLine(x, Chris@304: topMargin + labelRect.height() + spacing, Chris@304: x + boxWidth, Chris@304: topMargin + labelRect.height() + spacing); Chris@304: Chris@304: paint.drawText(QRect(x, Chris@304: topMargin, Chris@304: boxWidth, Chris@304: labelRect.height()), Chris@304: Qt::AlignCenter | Qt::TextWordWrap, Chris@304: label); Chris@304: } Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::setLayerDormant(const View *v, bool dormant) Chris@303: { Chris@303: if (dormant) { Chris@303: // Delete the images named in the view's scaled map from the Chris@303: // general image map as well. They can always be re-loaded Chris@303: // if it turns out another view still needs them. Chris@305: QMutexLocker locker(&m_imageMapMutex); Chris@303: for (ImageMap::iterator i = m_scaled[v].begin(); Chris@303: i != m_scaled[v].end(); ++i) { Chris@303: m_images.erase(i->first); Chris@303: } Chris@303: m_scaled.erase(v); Chris@303: } Chris@303: } Chris@303: Chris@303: //!!! how to reap no-longer-used images? Chris@303: Chris@304: bool Chris@304: ImageLayer::getImageOriginalSize(QString name, QSize &size) const Chris@303: { Chris@305: // std::cerr << "getImageOriginalSize: \"" << name.toStdString() << "\"" << std::endl; Chris@305: Chris@305: QMutexLocker locker(&m_imageMapMutex); Chris@303: if (m_images.find(name) == m_images.end()) { Chris@305: // std::cerr << "don't have, trying to open local" << std::endl; Chris@305: m_images[name] = QImage(getLocalFilename(name)); Chris@303: } Chris@304: if (m_images[name].isNull()) { Chris@305: // std::cerr << "null image" << std::endl; Chris@304: return false; Chris@304: } else { Chris@304: size = m_images[name].size(); Chris@304: return true; Chris@304: } Chris@303: } Chris@303: Chris@303: QImage Chris@303: ImageLayer::getImage(View *v, QString name, QSize maxSize) const Chris@303: { Chris@303: bool need = false; Chris@303: Chris@305: // std::cerr << "ImageLayer::getImage(" << v << ", " << name.toStdString() << ", (" Chris@305: // << maxSize.width() << "x" << maxSize.height() << "))" << std::endl; Chris@303: Chris@304: if (!m_scaled[v][name].isNull() && Chris@304: ((m_scaled[v][name].width() == maxSize.width() && Chris@304: m_scaled[v][name].height() <= maxSize.height()) || Chris@304: (m_scaled[v][name].width() <= maxSize.width() && Chris@304: m_scaled[v][name].height() == maxSize.height()))) { Chris@305: // std::cerr << "cache hit" << std::endl; Chris@303: return m_scaled[v][name]; Chris@303: } Chris@303: Chris@305: QMutexLocker locker(&m_imageMapMutex); Chris@305: Chris@303: if (m_images.find(name) == m_images.end()) { Chris@305: m_images[name] = QImage(getLocalFilename(name)); Chris@303: } Chris@303: Chris@303: if (m_images[name].isNull()) { Chris@305: // std::cerr << "null image" << std::endl; Chris@303: m_scaled[v][name] = QImage(); Chris@304: } else if (m_images[name].width() <= maxSize.width() && Chris@304: m_images[name].height() <= maxSize.height()) { Chris@304: m_scaled[v][name] = m_images[name]; Chris@303: } else { Chris@303: m_scaled[v][name] = Chris@303: m_images[name].scaled(maxSize, Chris@303: Qt::KeepAspectRatio, Chris@303: Qt::SmoothTransformation); Chris@303: } Chris@303: Chris@303: return m_scaled[v][name]; Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::drawStart(View *v, QMouseEvent *e) Chris@303: { Chris@303: // std::cerr << "ImageLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@303: Chris@303: if (!m_model) { Chris@303: std::cerr << "ImageLayer::drawStart: no model" << std::endl; Chris@303: return; Chris@303: } Chris@303: Chris@303: long frame = v->getFrameForX(e->x()); Chris@303: if (frame < 0) frame = 0; Chris@303: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@303: Chris@303: m_editingPoint = ImageModel::Point(frame, "", ""); Chris@303: m_originalPoint = m_editingPoint; Chris@303: Chris@303: if (m_editingCommand) m_editingCommand->finish(); Chris@303: m_editingCommand = new ImageModel::EditCommand(m_model, "Add Image"); Chris@303: m_editingCommand->addPoint(m_editingPoint); Chris@303: Chris@303: m_editing = true; Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::drawDrag(View *v, QMouseEvent *e) Chris@303: { Chris@303: // std::cerr << "ImageLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl; Chris@303: Chris@303: if (!m_model || !m_editing) return; Chris@303: Chris@303: long frame = v->getFrameForX(e->x()); Chris@303: if (frame < 0) frame = 0; Chris@303: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@303: Chris@303: m_editingCommand->deletePoint(m_editingPoint); Chris@303: m_editingPoint.frame = frame; Chris@303: m_editingCommand->addPoint(m_editingPoint); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::drawEnd(View *v, QMouseEvent *) Chris@303: { Chris@303: // std::cerr << "ImageLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@303: if (!m_model || !m_editing) return; Chris@303: Chris@303: bool ok = false; Chris@303: Chris@307: ImageDialog dialog(tr("Select image"), "", ""); Chris@305: Chris@303: if (dialog.exec() == QDialog::Accepted) { Chris@305: Chris@305: checkAddRemote(dialog.getImage()); Chris@305: Chris@303: ImageModel::ChangeImageCommand *command = Chris@303: new ImageModel::ChangeImageCommand Chris@303: (m_model, m_editingPoint, dialog.getImage(), dialog.getLabel()); Chris@303: m_editingCommand->addCommand(command); Chris@307: } else { Chris@307: m_editingCommand->deletePoint(m_editingPoint); Chris@303: } Chris@303: Chris@303: m_editingCommand->finish(); Chris@303: m_editingCommand = 0; Chris@303: m_editing = false; Chris@303: } Chris@303: Chris@312: bool Chris@312: ImageLayer::addImage(long frame, QString url) Chris@312: { Chris@312: QImage image(getLocalFilename(url)); Chris@312: if (image.isNull()) { Chris@312: delete m_remoteFiles[url]; Chris@312: m_remoteFiles.erase(url); Chris@312: return false; Chris@312: } Chris@312: Chris@312: ImageModel::Point point(frame, url, ""); Chris@312: ImageModel::EditCommand *command = Chris@312: new ImageModel::EditCommand(m_model, "Add Image"); Chris@312: command->addPoint(point); Chris@312: command->finish(); Chris@312: return true; Chris@312: } Chris@312: Chris@303: void Chris@303: ImageLayer::editStart(View *v, QMouseEvent *e) Chris@303: { Chris@303: // std::cerr << "ImageLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@303: Chris@303: if (!m_model) return; Chris@303: Chris@303: ImageModel::PointList points = getLocalPoints(v, e->x(), e->y()); Chris@303: if (points.empty()) return; Chris@303: Chris@303: m_editOrigin = e->pos(); Chris@303: m_editingPoint = *points.begin(); Chris@303: m_originalPoint = m_editingPoint; Chris@303: Chris@303: if (m_editingCommand) { Chris@303: m_editingCommand->finish(); Chris@303: m_editingCommand = 0; Chris@303: } Chris@303: Chris@303: m_editing = true; Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::editDrag(View *v, QMouseEvent *e) Chris@303: { Chris@303: if (!m_model || !m_editing) return; Chris@303: Chris@303: long frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x()); Chris@303: long frame = m_originalPoint.frame + frameDiff; Chris@303: Chris@303: if (frame < 0) frame = 0; Chris@303: frame = (frame / m_model->getResolution()) * m_model->getResolution(); Chris@303: Chris@303: if (!m_editingCommand) { Chris@303: m_editingCommand = new ImageModel::EditCommand(m_model, tr("Move Image")); Chris@303: } Chris@303: Chris@303: m_editingCommand->deletePoint(m_editingPoint); Chris@303: m_editingPoint.frame = frame; Chris@303: m_editingCommand->addPoint(m_editingPoint); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::editEnd(View *, QMouseEvent *) Chris@303: { Chris@303: // std::cerr << "ImageLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@303: if (!m_model || !m_editing) return; Chris@303: Chris@303: if (m_editingCommand) { Chris@303: m_editingCommand->finish(); Chris@303: } Chris@303: Chris@303: m_editingCommand = 0; Chris@303: m_editing = false; Chris@303: } Chris@303: Chris@303: bool Chris@303: ImageLayer::editOpen(View *v, QMouseEvent *e) Chris@303: { Chris@303: if (!m_model) return false; Chris@303: Chris@303: ImageModel::PointList points = getLocalPoints(v, e->x(), e->y()); Chris@303: if (points.empty()) return false; Chris@303: Chris@303: QString image = points.begin()->image; Chris@303: QString label = points.begin()->label; Chris@303: Chris@303: ImageDialog dialog(tr("Select image"), Chris@303: image, Chris@303: label); Chris@303: Chris@303: if (dialog.exec() == QDialog::Accepted) { Chris@305: Chris@305: checkAddRemote(dialog.getImage()); Chris@305: Chris@303: ImageModel::ChangeImageCommand *command = Chris@303: new ImageModel::ChangeImageCommand Chris@303: (m_model, *points.begin(), dialog.getImage(), dialog.getLabel()); Chris@305: Chris@303: CommandHistory::getInstance()->addCommand(command); Chris@303: } Chris@303: Chris@303: return true; Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::moveSelection(Selection s, size_t newStartFrame) Chris@303: { Chris@303: if (!m_model) return; Chris@303: Chris@303: ImageModel::EditCommand *command = Chris@303: new ImageModel::EditCommand(m_model, tr("Drag Selection")); Chris@303: Chris@303: ImageModel::PointList points = Chris@303: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@303: Chris@303: for (ImageModel::PointList::iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: Chris@303: if (s.contains(i->frame)) { Chris@303: ImageModel::Point newPoint(*i); Chris@303: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@303: command->deletePoint(*i); Chris@303: command->addPoint(newPoint); Chris@303: } Chris@303: } Chris@303: Chris@303: command->finish(); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::resizeSelection(Selection s, Selection newSize) Chris@303: { Chris@303: if (!m_model) return; Chris@303: Chris@303: ImageModel::EditCommand *command = Chris@303: new ImageModel::EditCommand(m_model, tr("Resize Selection")); Chris@303: Chris@303: ImageModel::PointList points = Chris@303: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@303: Chris@303: double ratio = Chris@303: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@303: double(s.getEndFrame() - s.getStartFrame()); Chris@303: Chris@303: for (ImageModel::PointList::iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: Chris@303: if (s.contains(i->frame)) { Chris@303: Chris@303: double target = i->frame; Chris@303: target = newSize.getStartFrame() + Chris@303: double(target - s.getStartFrame()) * ratio; Chris@303: Chris@303: ImageModel::Point newPoint(*i); Chris@303: newPoint.frame = lrint(target); Chris@303: command->deletePoint(*i); Chris@303: command->addPoint(newPoint); Chris@303: } Chris@303: } Chris@303: Chris@303: command->finish(); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::deleteSelection(Selection s) Chris@303: { Chris@303: if (!m_model) return; Chris@303: Chris@303: ImageModel::EditCommand *command = Chris@303: new ImageModel::EditCommand(m_model, tr("Delete Selection")); Chris@303: Chris@303: ImageModel::PointList points = Chris@303: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@303: Chris@303: for (ImageModel::PointList::iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: if (s.contains(i->frame)) command->deletePoint(*i); Chris@303: } Chris@303: Chris@303: command->finish(); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::copy(Selection s, Clipboard &to) Chris@303: { Chris@303: if (!m_model) return; Chris@303: Chris@303: ImageModel::PointList points = Chris@303: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@303: Chris@303: for (ImageModel::PointList::iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: if (s.contains(i->frame)) { Chris@303: //!!! inadequate Chris@303: Clipboard::Point point(i->frame, i->label); Chris@303: to.addPoint(point); Chris@303: } Chris@303: } Chris@303: } Chris@303: Chris@303: bool Chris@303: ImageLayer::paste(const Clipboard &from, int frameOffset, bool /* interactive */) Chris@303: { Chris@303: if (!m_model) return false; Chris@303: Chris@303: const Clipboard::PointList &points = from.getPoints(); Chris@303: Chris@303: ImageModel::EditCommand *command = Chris@303: new ImageModel::EditCommand(m_model, tr("Paste")); Chris@303: Chris@303: for (Clipboard::PointList::const_iterator i = points.begin(); Chris@303: i != points.end(); ++i) { Chris@303: Chris@303: if (!i->haveFrame()) continue; Chris@303: size_t frame = 0; Chris@303: if (frameOffset > 0 || -frameOffset < i->getFrame()) { Chris@303: frame = i->getFrame() + frameOffset; Chris@303: } Chris@303: ImageModel::Point newPoint(frame); Chris@303: Chris@303: //!!! inadequate Chris@303: Chris@303: if (i->haveLabel()) { Chris@303: newPoint.label = i->getLabel(); Chris@303: } else if (i->haveValue()) { Chris@303: newPoint.label = QString("%1").arg(i->getValue()); Chris@303: } else { Chris@303: newPoint.label = tr("New Point"); Chris@303: } Chris@303: Chris@303: command->addPoint(newPoint); Chris@303: } Chris@303: Chris@303: command->finish(); Chris@303: return true; Chris@303: } Chris@303: Chris@303: QString Chris@305: ImageLayer::getLocalFilename(QString img) const Chris@305: { Chris@305: if (m_remoteFiles.find(img) == m_remoteFiles.end()) { Chris@305: checkAddRemote(img); Chris@312: if (m_remoteFiles.find(img) == m_remoteFiles.end()) { Chris@312: return img; Chris@312: } Chris@305: } Chris@305: return m_remoteFiles[img]->getLocalFilename(); Chris@305: } Chris@305: Chris@305: void Chris@305: ImageLayer::checkAddRemote(QString img) const Chris@305: { Chris@318: if (FileSource::isRemote(img)) { Chris@305: Chris@312: std::cerr << "ImageLayer::checkAddRemote(" << img.toStdString() << "): yes, trying..." << std::endl; Chris@312: Chris@305: if (m_remoteFiles.find(img) != m_remoteFiles.end()) { Chris@305: return; Chris@305: } Chris@305: Chris@318: FileSource *rf = new FileSource(img); Chris@317: if (rf->isOK()) { Chris@317: std::cerr << "ok, adding it (local filename = " << rf->getLocalFilename().toStdString() << ")" << std::endl; Chris@317: m_remoteFiles[img] = rf; Chris@317: connect(rf, SIGNAL(ready()), this, SLOT(remoteFileReady())); Chris@317: } else { Chris@317: delete rf; Chris@305: } Chris@305: } Chris@305: } Chris@305: Chris@305: void Chris@305: ImageLayer::checkAddRemotes() Chris@305: { Chris@305: const ImageModel::PointList &points(m_model->getPoints()); Chris@305: Chris@305: for (ImageModel::PointList::const_iterator i = points.begin(); Chris@305: i != points.end(); ++i) { Chris@305: Chris@305: checkAddRemote((*i).image); Chris@305: } Chris@305: } Chris@305: Chris@305: void Chris@305: ImageLayer::remoteFileReady() Chris@305: { Chris@305: // std::cerr << "ImageLayer::remoteFileReady" << std::endl; Chris@305: Chris@318: FileSource *rf = dynamic_cast(sender()); Chris@305: if (!rf) return; Chris@305: Chris@305: QString img; Chris@318: for (FileSourceMap::const_iterator i = m_remoteFiles.begin(); Chris@305: i != m_remoteFiles.end(); ++i) { Chris@305: if (i->second == rf) { Chris@305: img = i->first; Chris@305: // std::cerr << "it's image \"" << img.toStdString() << "\"" << std::endl; Chris@305: break; Chris@305: } Chris@305: } Chris@305: if (img == "") return; Chris@305: Chris@305: QMutexLocker locker(&m_imageMapMutex); Chris@305: m_images.erase(img); Chris@305: for (ViewImageMap::iterator i = m_scaled.begin(); i != m_scaled.end(); ++i) { Chris@305: i->second.erase(img); Chris@306: emit modelChanged(); Chris@305: } Chris@305: } Chris@305: Chris@316: void Chris@316: ImageLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@303: { Chris@316: Layer::toXml(stream, indent, extraAttributes); Chris@303: } Chris@303: Chris@303: void Chris@303: ImageLayer::setProperties(const QXmlAttributes &attributes) Chris@303: { Chris@303: } Chris@303: