PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sheetcanvas/ClipsViewPort.cpp

#
C++ | 211 lines | 144 code | 37 blank | 30 comment | 17 complexity | 740f4a4551ba860cf9683d449e74f552 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2006-2007 Remon Sijrier
  3. This file is part of Traverso
  4. Traverso is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "ClipsViewPort.h"
  17. #include "SheetWidget.h"
  18. #include "SheetView.h"
  19. #include "AudioTrackView.h"
  20. #include "ViewItem.h"
  21. #include <libtraversocore.h>
  22. #include <Import.h>
  23. #include <CommandGroup.h>
  24. #include "RemoveClip.h"
  25. #include "AudioDevice.h"
  26. #include <QScrollBar>
  27. #include <QSet>
  28. #include <QPaintEngine>
  29. #include <QUrl>
  30. #include <QFileInfo>
  31. #include <QDir>
  32. #include <QDragEnterEvent>
  33. #include <Debugger.h>
  34. ClipsViewPort::ClipsViewPort(QGraphicsScene* scene, SheetWidget* sw)
  35. : ViewPort(scene, sw)
  36. {
  37. m_sw = sw;
  38. viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
  39. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  40. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  41. scale(1.0, 1.0);
  42. }
  43. void ClipsViewPort::resizeEvent( QResizeEvent * e )
  44. {
  45. ViewPort::resizeEvent(e);
  46. // m_sw->get_sheetview()->clipviewport_resize_event();
  47. }
  48. void ClipsViewPort::paintEvent(QPaintEvent * e)
  49. {
  50. QGraphicsView::paintEvent(e);
  51. }
  52. void ClipsViewPort::dragEnterEvent( QDragEnterEvent * event )
  53. {
  54. m_imports.clear();
  55. m_resourcesImport.clear();
  56. // let's see if the D&D was from the resources bin.
  57. if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
  58. QByteArray encodedData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
  59. QDataStream stream(&encodedData, QIODevice::ReadOnly);
  60. int r, c;
  61. QMap<int, QVariant> v;
  62. while (!stream.atEnd()) {
  63. stream >> r >> c >> v;
  64. qint64 id = v.value(Qt::UserRole).toLongLong();
  65. if (!id) {
  66. continue;
  67. }
  68. m_resourcesImport.append(id);
  69. }
  70. }
  71. // and who knows, it could have been a D&D drop from a filemanager...
  72. if (event->mimeData()->hasUrls()) {
  73. foreach(QUrl url, event->mimeData()->urls()) {
  74. QString fileName = url.toLocalFile();
  75. if (fileName.isEmpty()) {
  76. continue;
  77. }
  78. Import* import = new Import(fileName);
  79. m_imports.append(import);
  80. // If a readsource fails to init, the D&D should be
  81. // marked as failed, cleanup allready created imports,
  82. // and clear the import list.
  83. if (import->create_readsource() == -1) {
  84. foreach(Import* import, m_imports) {
  85. delete import;
  86. }
  87. m_imports.clear();
  88. break;
  89. }
  90. }
  91. }
  92. if (m_imports.size() || m_resourcesImport.size()) {
  93. event->acceptProposedAction();
  94. }
  95. }
  96. void ClipsViewPort::dropEvent(QDropEvent* event )
  97. {
  98. PENTER;
  99. Q_UNUSED(event)
  100. if (!m_importTrack) {
  101. return;
  102. }
  103. CommandGroup* group = new CommandGroup(m_sw->get_sheet(),
  104. tr("Import %n audiofile(s)", "", m_imports.size() + m_resourcesImport.size()), true);
  105. TimeRef startpos = TimeRef(mapFromGlobal(QCursor::pos()).x() * m_sw->get_sheetview()->timeref_scalefactor);
  106. foreach(qint64 id, m_resourcesImport) {
  107. AudioClip* clip = resources_manager()->get_clip(id);
  108. if (clip) {
  109. bool hadSheet = clip->has_sheet();
  110. clip->set_sheet(((Sheet*)m_sw->get_sheet()));
  111. clip->set_track(m_importTrack);
  112. if (!hadSheet) {
  113. clip->set_state(clip->get_dom_node());
  114. }
  115. clip->set_track_start_location(startpos);
  116. startpos = clip->get_track_end_location();
  117. AddRemoveClip* arc = new AddRemoveClip(clip, AddRemoveClip::ADD);
  118. group->add_command(arc);
  119. continue;
  120. }
  121. ReadSource* source = resources_manager()->get_readsource(id);
  122. if (source) {
  123. clip = resources_manager()->new_audio_clip(source->get_short_name());
  124. resources_manager()->set_source_for_clip(clip, source);
  125. clip->set_sheet(m_importTrack->get_sheet());
  126. clip->set_track(m_importTrack);
  127. clip->set_track_start_location(startpos);
  128. startpos = clip->get_track_end_location();
  129. AddRemoveClip* arc = new AddRemoveClip(clip, AddRemoveClip::ADD);
  130. group->add_command(arc);
  131. }
  132. }
  133. bool firstItem = true;
  134. foreach(Import* import, m_imports) {
  135. import->set_track(m_importTrack);
  136. if (firstItem) {
  137. // Place first item at cursor, others at end of track.
  138. import->set_position(startpos);
  139. firstItem = false;
  140. }
  141. group->add_command(import);
  142. }
  143. TCommand::process_command(group);
  144. }
  145. void ClipsViewPort::dragMoveEvent( QDragMoveEvent * event )
  146. {
  147. Q_UNUSED(event)
  148. Project* project = pm().get_project();
  149. if (!project) {
  150. return;
  151. }
  152. Sheet* sheet = qobject_cast<Sheet*>(project->get_current_session());
  153. if (!sheet) {
  154. return;
  155. }
  156. m_importTrack = 0;
  157. // hmmm, code below is candidate for improvements...?
  158. // no mouse move events during D&D move events...
  159. // So we need to calculate the scene pos ourselves.
  160. QPointF mouseposTosScene = mapFromGlobal(QCursor::pos());
  161. QList<QGraphicsItem *> itemlist = items((int)mouseposTosScene.x(), (int)mouseposTosScene.y());
  162. foreach(QGraphicsItem* obj, itemlist) {
  163. AudioTrackView* tv = dynamic_cast<AudioTrackView*>(obj);
  164. if (tv) {
  165. m_importTrack = tv->get_track();
  166. return;
  167. }
  168. }
  169. }