Add default copy-paste handlers for RDListWidget and RDTreeWidget

This commit is contained in:
baldurk
2017-05-30 16:13:41 +01:00
parent 4d8ee2be43
commit c95f7c4cea
6 changed files with 110 additions and 3 deletions
@@ -23,7 +23,10 @@
******************************************************************************/
#include "RDListWidget.h"
#include <QApplication>
#include <QClipboard>
#include <QMouseEvent>
#include "Code/Interface/QRDInterface.h"
RDListWidget::RDListWidget(QWidget *parent) : QListWidget(parent)
{
@@ -44,3 +47,29 @@ void RDListWidget::mouseDoubleClickEvent(QMouseEvent *event)
emit(mouseDoubleClicked(event));
QListWidget::mouseDoubleClickEvent(event);
}
void RDListWidget::keyPressEvent(QKeyEvent *event)
{
if(!m_customCopyPaste && event->matches(QKeySequence::Copy))
{
QList<QListWidgetItem *> items = selectedItems();
std::sort(items.begin(), items.end(),
[this](QListWidgetItem *a, QListWidgetItem *b) { return row(a) < row(b); });
QString clipboardText;
for(QListWidgetItem *i : items)
{
clipboardText += i->text() + lit("\n");
}
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(clipboardText.trimmed());
}
else
{
QListWidget::keyPressEvent(event);
}
emit(keyPress(event));
}
@@ -29,17 +29,24 @@ class RDListWidget : public QListWidget
{
private:
Q_OBJECT
Q_PROPERTY(bool customCopyPasteHandler READ customCopyPasteHandler WRITE setCustomCopyPasteHandler)
public:
explicit RDListWidget(QWidget *parent = 0);
~RDListWidget();
bool customCopyPasteHandler() { return m_customCopyPaste; }
void setCustomCopyPasteHandler(bool custom) { m_customCopyPaste = custom; }
signals:
void mouseClicked(QMouseEvent *event);
void mouseDoubleClicked(QMouseEvent *event);
void keyPress(QKeyEvent *event);
public slots:
protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
bool m_customCopyPaste = false;
};
+57 -1
View File
@@ -23,6 +23,8 @@
******************************************************************************/
#include "RDTreeWidget.h"
#include <QApplication>
#include <QClipboard>
#include <QColor>
#include <QDebug>
#include <QMouseEvent>
@@ -30,6 +32,7 @@
#include <QPen>
#include <QStack>
#include <QToolTip>
#include "Code/Interface/QRDInterface.h"
class RDTreeWidgetModel : public QAbstractItemModel
{
@@ -639,8 +642,61 @@ void RDTreeWidget::focusOutEvent(QFocusEvent *event)
void RDTreeWidget::keyPressEvent(QKeyEvent *e)
{
if(!m_customCopyPaste && e->matches(QKeySequence::Copy))
{
QModelIndexList sel = selectionModel()->selectedRows();
int stackWidths[16];
int *heapWidths = NULL;
int colCount = m_model->columnCount();
if(colCount >= 16)
heapWidths = new int[colCount];
int *widths = heapWidths ? heapWidths : stackWidths;
for(int i = 0; i < colCount; i++)
widths[i] = 0;
// align the copied data so that each column is the same width
for(QModelIndex idx : sel)
{
RDTreeWidgetItem *item = m_model->itemForIndex(idx);
for(int i = 0; i < qMin(colCount, item->m_text.count()); i++)
widths[i] = qMax(widths[i], item->m_text[i].toString().count());
}
// only align up to 50 characters so one really long item doesn't mess up the whole thing
for(int i = 0; i < colCount; i++)
widths[i] = qMin(50, widths[i]);
QString clipData;
for(QModelIndex idx : sel)
{
RDTreeWidgetItem *item = m_model->itemForIndex(idx);
for(int i = 0; i < qMin(colCount, item->m_text.count()); i++)
{
QString format = i == 0 ? QFormatStr("%1") : QFormatStr(" %1");
clipData += format.arg(item->m_text[i].toString(), -widths[i]);
}
clipData += lit("\n");
}
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(clipData.trimmed());
delete[] heapWidths;
}
else
{
QTreeView::keyPressEvent(e);
}
emit(keyPress(e));
QTreeView::keyPressEvent(e);
}
void RDTreeWidget::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const
@@ -150,6 +150,7 @@ class RDTreeWidget : public QTreeView
Q_OBJECT
Q_PROPERTY(bool instantTooltips READ instantTooltips WRITE setInstantTooltips)
Q_PROPERTY(bool customCopyPasteHandler READ customCopyPasteHandler WRITE setCustomCopyPasteHandler)
public:
explicit RDTreeWidget(QWidget *parent = 0);
~RDTreeWidget();
@@ -167,6 +168,8 @@ public:
void setClearSelectionOnFocusLoss(bool clear) { m_clearSelectionOnFocusLoss = clear; }
bool instantTooltips() { return m_instantTooltips; }
void setInstantTooltips(bool instant) { m_instantTooltips = instant; }
bool customCopyPasteHandler() { return m_customCopyPaste; }
void setCustomCopyPasteHandler(bool custom) { m_customCopyPaste = custom; }
RDTreeWidgetItem *invisibleRootItem() { return m_root; }
void addTopLevelItem(RDTreeWidgetItem *item) { m_root->addChild(item); }
RDTreeWidgetItem *topLevelItem(int index) const { return m_root->child(index); }
@@ -241,6 +244,7 @@ private:
RDTreeWidgetItem *m_currentHoverItem = NULL;
bool m_instantTooltips = false;
bool m_customCopyPaste = false;
int m_hoverColumn = -1;
QIcon m_normalHoverIcon;
QIcon m_activeHoverIcon;
+12 -1
View File
@@ -47,6 +47,9 @@
<property name="alternatingRowColors">
<bool>false</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ContiguousSelection</enum>
</property>
<property name="indentation">
<number>20</number>
</property>
@@ -60,13 +63,16 @@
<bool>true</bool>
</property>
</widget>
<widget class="QListWidget" name="callstack">
<widget class="RDListWidget" name="callstack">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ContiguousSelection</enum>
</property>
</widget>
</widget>
</item>
@@ -84,6 +90,11 @@
<header>Widgets/Extended/RDSplitter.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>RDListWidget</class>
<extends>QListWidget</extends>
<header>Widgets/Extended/RDListWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
+1 -1
View File
@@ -2253,7 +2253,7 @@ void TextureViewer::render_keyPress(QKeyEvent *e)
if(texptr == NULL)
return;
if((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_C)
if(e->matches(QKeySequence::Copy))
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(ui->texStatusDim->text() + lit(" | ") + ui->statusText->text());