tuchulcha  0.10.1
Graphical Workflow Configuration Editor
QCopyListView.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17  */
24 #include "QCopyListView.h"
25 
26 #include <QKeyEvent>
27 #include <QApplication>
28 #include <QClipboard>
29 #include <QTextCodec>
30 #include <QMimeData>
31 
33  QListView(pp)
34 {
35 }
36 
37 QCopyListView::~QCopyListView() {
38 }
39 
41  QStringList linesT,linesH;
42  QModelIndexList selection = selectedIndexes();
43  QListIterator<QModelIndex> selIter(selection);
44  while (selIter.hasNext()) {
45  QModelIndex curIdx = selIter.next();
46  QVariant curData; QString colStyle;
47  curData = model()->data(curIdx,Qt::ForegroundRole);
48  if (curData.type() == QVariant::Brush) {
49  QBrush brush = qvariant_cast<QBrush>(curData);
50  QString color = brush.color().name();
51  if (!color.isEmpty()) {
52  colStyle += QString("color:%1;").arg(color);
53  }
54  }
55  curData = model()->data(curIdx,Qt::BackgroundRole);
56  if (curData.type() == QVariant::Brush) {
57  QBrush brush = qvariant_cast<QBrush>(curData);
58  QString color = brush.color().name();
59  if (!color.isEmpty()) {
60  colStyle += QString("background-color:%1;").arg(color);
61  }
62  }
63  curData = model()->data(curIdx,Qt::DisplayRole);
64  if (curData.type() == QVariant::String) {
65  QString curLine = curData.toString();
66  linesT << curLine;
67  // replace non-compatible characters with html equivalents
68  curLine.replace("&","&amp;");
69  curLine.replace("\"","&quot;");
70  curLine.replace("<","&lt;");
71  curLine.replace(">","&gt;");
72  linesH <<
73  QString("<span class=\"logLine\" style=\"%1\">%2</span>")
74  .arg(colStyle).arg(curLine);
75  }
76  }
77 
78  QMimeData* clipData = new QMimeData;
79  clipData->setText(linesT.join("\n").toLocal8Bit());
80  QString htmlCont = QString(
81  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
82  "\"http://www.w3.org/TR/html4/strict.dtd\">\n"
83  "<html>\n<head>\n<title>Tuchulcha Log</title>\n"
84  "<meta http-equiv=\"content-type\" "
85  "content=\"text/html; charset=UTF-8\">\n"
86  "<style type=\"text/css\">"
87  "span.logLine{font-family:monospace;white-space:pre-wrap;"
88  "-moz-tab-size:4;-o-tab-size:4;tab-size:4;}"
89  "</style>\n</head>\n"
90  "<body>\n<div>\n%1\n</div>\n</body>\n</html>")
91  .arg(linesH.join("<br>\n"));
92  clipData->setHtml(htmlCont);
93  return clipData;
94 }
95 
96 void QCopyListView::keyPressEvent(QKeyEvent* ev) {
97  // based on the QT code from AbstractItemView::keyPressEvent
98 #if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
99  if (ev == QKeySequence::Copy) {
100  QApplication::clipboard()->setMimeData(getSelectedContent());
101  ev->accept();
102  }
103  else {
104  QListView::keyPressEvent(ev);
105  }
106 #else
107  QListView::keyPressEvent(ev);
108 #endif
109 }
QMimeData * getSelectedContent() const
get mime-data representation of selected indices
Declaration of class QCopyListView.
QCopyListView(QWidget *parent=0)
default constructor
virtual void keyPressEvent(QKeyEvent *event)
reworked key press event handler able to copy multipe lines into the clipboard