kwave  18.07.70
ClipBoard.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ClipBoard.cpp - the Kwave clipboard
3  -------------------
4  begin : Tue Jun 26, 2001
5  copyright : (C) 2001 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7 
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "config.h"
20 
21 #include <new>
22 
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QList>
26 #include <QReadLocker>
27 #include <QWriteLocker>
28 
29 #include "libkwave/ClipBoard.h"
30 #include "libkwave/CodecManager.h"
31 #include "libkwave/MimeData.h"
33 #include "libkwave/SignalManager.h"
34 
37 
38 //***************************************************************************
40 {
41  return g_clipboard;
42 }
43 
44 //***************************************************************************
46  :m_tracks(0)
47 {
48 }
49 
50 //***************************************************************************
52 {
53  // clear() must have been before, e.g. in the application's destructor !
54 }
55 
56 //***************************************************************************
57 void Kwave::ClipBoard::slotChanged(QClipboard::Mode mode)
58 {
59  if (mode != QClipboard::Clipboard) return;
60  bool data_available = !isEmpty();
61 
62 // qDebug("=> ClipBoard::clipboardChanged(%s)",
63 // data_available ? "FULL" : "EMPTY");
64 
65  emit clipboardChanged(data_available);
66 }
67 
68 //***************************************************************************
69 void Kwave::ClipBoard::copy(QWidget *widget,
70  Kwave::SignalManager &signal_manager,
71  const QList<unsigned int> &track_list,
72  sample_index_t offset, sample_index_t length)
73 {
74  // break if nothing to do
75  if (!length || !track_list.count()) return;
76 
77  // get a buffer, implemented as a KwaveMimeData container
78  Kwave::MimeData *buffer = new(std::nothrow) Kwave::MimeData();
79  Q_ASSERT(buffer);
80  if (!buffer) return;
81 
82  // encode into the mime data container
84  track_list, offset, offset + length - 1);
85 
86  if (!buffer->encode(widget, src, signal_manager.metaData())) {
87  // encoding failed, reset to empty
88  buffer->clear();
89  delete buffer;
90  return;
91  }
92 
93  // give the buffer to the KDE clipboard
94  qApp->processEvents();
95  QApplication::clipboard()->setMimeData(buffer, QClipboard::Clipboard);
96 }
97 
98 //***************************************************************************
99 bool Kwave::ClipBoard::paste(QWidget *widget,
100  Kwave::SignalManager &signal_manager,
101  sample_index_t offset, sample_index_t length)
102 {
103  Q_ASSERT(!isEmpty());
104  if (isEmpty()) return false; // clipboard is empty ?
105 
106  // delete the current selection (with undo)
107  if (length <= 1) length = 0; // do not paste single samples !
108  if (length && !signal_manager.deleteRange(offset, length))
109  return false;
110 
111  sample_index_t decoded_samples = Kwave::MimeData::decode(
112  widget,
113  QApplication::clipboard()->mimeData(QClipboard::Clipboard),
114  signal_manager,
115  offset);
116  if (!decoded_samples) return false;
117 
118  // set the selection to the inserted range
119  signal_manager.selectRange(offset, decoded_samples);
120  return true;
121 }
122 
123 //***************************************************************************
125 {
126  QApplication::clipboard()->clear();
127 }
128 
129 //***************************************************************************
131 {
132  const QMimeData *mime_data =
133  QApplication::clipboard()->mimeData(QClipboard::Clipboard);
134 
135  // no mime data -> empty
136  if (!mime_data) return true;
137 
138  // there is a format that we can decode -> not empty
139  foreach (const QString &format, mime_data->formats())
140  if (Kwave::CodecManager::canDecode(format)) return false;
141 
142  // nothing to decode -> empty
143  return true;
144 }
145 
146 //***************************************************************************
147 //***************************************************************************
virtual void clear()
Definition: MimeData.cpp:434
void selectRange(sample_index_t offset, sample_index_t length)
static ClipBoard & instance()
Definition: ClipBoard.cpp:39
Kwave::MetaDataList & metaData()
void copy(QWidget *widget, Kwave::SignalManager &signal_manager, const QList< unsigned int > &track_list, sample_index_t offset, sample_index_t length)
Definition: ClipBoard.cpp:69
quint64 sample_index_t
Definition: Sample.h:28
bool deleteRange(sample_index_t offset, sample_index_t length, const QList< unsigned int > &track_list)
void slotChanged(QClipboard::Mode mode)
Definition: ClipBoard.cpp:57
virtual ~ClipBoard()
Definition: ClipBoard.cpp:51
static sample_index_t decode(QWidget *widget, const QMimeData *e, Kwave::SignalManager &sig, sample_index_t pos)
Definition: MimeData.cpp:237
unsigned int m_tracks
Definition: ClipBoard.h:115
bool paste(QWidget *widget, Kwave::SignalManager &signal_manager, sample_index_t offset, sample_index_t length)
Definition: ClipBoard.cpp:99
virtual bool encode(QWidget *widget, Kwave::MultiTrackReader &src, const Kwave::MetaDataList &meta_data)
Definition: MimeData.cpp:184
void clipboardChanged(bool data_available)
static bool canDecode(const QString &mimetype_name)
static Kwave::ClipBoard g_clipboard
Definition: ClipBoard.cpp:36