Add extended RDTableWidget with row re-ordering

This commit is contained in:
baldurk
2017-02-06 17:12:41 +00:00
parent baf2c9fa90
commit 95d84f2da7
5 changed files with 144 additions and 2 deletions
@@ -0,0 +1,86 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2017 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "RDTableWidget.h"
#include <QDropEvent>
RDTableWidget::RDTableWidget(QWidget *parent) : QTableWidget(parent)
{
}
void RDTableWidget::dropEvent(QDropEvent *event)
{
if(event->source() == this && model()->supportedDropActions() & event->dropAction())
{
QModelIndex index;
if(viewport()->rect().contains(event->pos()))
index = indexAt(event->pos());
// ignore no-op drops (same source and dest row)
if(selectedIndexes().contains(index))
return;
QRect rect = visualRect(index);
int halfway = rect.top() + rect.height() / 2;
// only allow below or above dropping
int row = index.row();
if(event->pos().y() > halfway)
row++;
// verify we can drop past this row (bit of a hack)
{
QTableWidgetItem *src = item(qMin(row, rowCount() - 1), 0);
if(src && !(src->flags() & Qt::ItemIsDropEnabled))
return;
}
insertRow(row);
int srcRow = selectedIndexes()[0].row();
// copy data across
for(int i = 0; i < columnCount(); i++)
{
QTableWidgetItem *src = item(srcRow, i);
if(src)
setItem(row, i, new QTableWidgetItem(*item(srcRow, i)));
else
setCellWidget(row, i, cellWidget(srcRow, i));
}
removeRow(srcRow);
return;
}
return QTableWidget::dropEvent(event);
}
void RDTableWidget::keyPressEvent(QKeyEvent *e)
{
QTableView::keyPressEvent(e);
emit(keyPress(e));
}
@@ -0,0 +1,42 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2017 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#pragma once
#include <QTableWidget>
class RDTableWidget : public QTableWidget
{
Q_OBJECT
public:
explicit RDTableWidget(QWidget *parent = 0);
signals:
void keyPress(QKeyEvent *e);
private:
// implement reordering row drag-drop behaviour
void dropEvent(QDropEvent *event) override;
void keyPressEvent(QKeyEvent *e) override;
};
+4 -2
View File
@@ -134,7 +134,8 @@ SOURCES += Code/qrenderdoc.cpp \
Widgets/Extended/RDTableView.cpp \
Windows/DebugMessageView.cpp \
Windows/StatisticsViewer.cpp \
Windows/Dialogs/SettingsDialog.cpp
Windows/Dialogs/SettingsDialog.cpp \
Widgets/Extended/RDTableWidget.cpp
HEADERS += Code/CaptureContext.h \
Code/qprocessinfo.h \
@@ -173,7 +174,8 @@ HEADERS += Code/CaptureContext.h \
Widgets/Extended/RDTableView.h \
Windows/DebugMessageView.h \
Windows/StatisticsViewer.h \
Windows/Dialogs/SettingsDialog.h
Windows/Dialogs/SettingsDialog.h \
Widgets/Extended/RDTableWidget.h
FORMS += Windows/Dialogs/AboutDialog.ui \
Windows/MainWindow.ui \
+3
View File
@@ -513,6 +513,7 @@
<ClCompile Include="generated\moc_RDListView.cpp" />
<ClCompile Include="generated\moc_RDListWidget.cpp" />
<ClCompile Include="generated\moc_RDTableView.cpp" />
<ClCompile Include="generated\moc_RDTableWidget.cpp" />
<ClCompile Include="generated\moc_RDTreeWidget.cpp" />
<ClCompile Include="generated\moc_ResourcePreview.cpp" />
<ClCompile Include="generated\moc_ScintillaDocument.cpp" />
@@ -534,6 +535,7 @@
<ClCompile Include="Widgets\BufferFormatSpecifier.cpp" />
<ClCompile Include="Widgets\Extended\RDListWidget.cpp" />
<ClCompile Include="Widgets\Extended\RDTableView.cpp" />
<ClCompile Include="Widgets\Extended\RDTableWidget.cpp" />
<ClCompile Include="Widgets\Extended\RDTreeWidget.cpp" />
<ClCompile Include="Widgets\RangeHistogram.cpp" />
<ClCompile Include="Widgets\Extended\RDDoubleSpinBox.cpp" />
@@ -674,6 +676,7 @@
<ClInclude Include="Widgets\BufferFormatSpecifier.h" />
<ClInclude Include="Widgets\Extended\RDListWidget.h" />
<ClInclude Include="Widgets\Extended\RDTableView.h" />
<ClInclude Include="Widgets\Extended\RDTableWidget.h" />
<ClInclude Include="Widgets\Extended\RDTreeWidget.h" />
<ClInclude Include="Widgets\RangeHistogram.h" />
<ClInclude Include="Widgets\Extended\RDDoubleSpinBox.h" />
@@ -484,6 +484,12 @@
<ClCompile Include="generated\moc_SettingsDialog.cpp">
<Filter>Generated Files</Filter>
</ClCompile>
<ClCompile Include="Widgets\Extended\RDTableWidget.cpp">
<Filter>Widgets\Extended</Filter>
</ClCompile>
<ClCompile Include="generated\moc_RDTableWidget.cpp">
<Filter>Generated Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="3rdparty\flowlayout\FlowLayout.h">
@@ -861,6 +867,9 @@
<ClInclude Include="generated\ui_SettingsDialog.h">
<Filter>Generated Files</Filter>
</ClInclude>
<ClInclude Include="Widgets\Extended\RDTableWidget.h">
<Filter>Widgets\Extended</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="resources.qrc">