Skip to content

Commit

Permalink
Added ctrl+c functionality by subclassing QTableView
Browse files Browse the repository at this point in the history
  • Loading branch information
AmericanEnglish committed Nov 23, 2017
1 parent 6d5fd04 commit 2167659
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
1 change: 0 additions & 1 deletion cpp/rsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void rSearch::run() {
// Thread Saftey at its finest
complete[i] = true;
}
// #pragma omp barrier
if (*stopped) {
qDebug() << "=rSearch (Old): Successfully killed!";
}
Expand Down
52 changes: 50 additions & 2 deletions cpp/widgets/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <QWidget>
#include <QHeaderView>
#include <QTimer>
#include <QApplication>
#include <QClipboard>
#include <QAbstractItemView>
#include "search.h"
#include "rsearch.h"

Expand Down Expand Up @@ -80,7 +83,8 @@ void Reader::initGui() {
setWindowTitle("PSO2 Chat Reader");
resize(900, 500);
// Build Table
table = new QTableView(this);
// table = new QTableView(this);
table = new SimpleTableView(this);
table->setSortingEnabled(false);
// (table->verticalHeader)()->setVisible(false);
QHeaderView *vv = table->verticalHeader();
Expand Down Expand Up @@ -298,7 +302,7 @@ QList<QStringList> Reader::digestFile(QString filename) {
if (len == 6) {
QStringList usable;
// Time
usable << line.at(0).split("T").at(0);
usable << line.at(0).split("T").at(1);
// IDs
usable << line.at(4) + QString("\n") + line.at(3);
// Message
Expand Down Expand Up @@ -379,3 +383,47 @@ QVariant ChatTable::headerData(int section, Qt::Orientation orientation, int rol
}
return QVariant();
}

SimpleTableView::SimpleTableView(QWidget *parent) : QTableView(parent) {
setSelectionBehavior(QAbstractItemView::SelectRows);
}

void SimpleTableView::keyPressEvent(QKeyEvent *event) {
// If Ctrl-C typed
// Or use event->matches(QKeySequence::Copy)
if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
{
QModelIndexList cells = this->selectionModel()->selectedIndexes();
qDebug() << "+Reader: Copy count" << cells.count();
qSort(cells); // Necessary, otherwise they are in column order
QString toClip;
// QModelIndex cell;
for (int i = 0; i < cells.count() / 3; i++) {
// qDebug() << cells.at(i).row() << cells.at(i).column();
// First column
toClip += "[" + cells.at(3*i + 0).data().toString() + "] ";
// Second column
toClip += cells.at(3*i + 1).data().toString().split(QRegExp("\\s+")).at(0) + ": ";
// Third column
toClip += cells.at(3*i+2).data().toString() + "\n";
}
// QString text;
// int currentRow = 0; // To determine when to insert newlines
// foreach (const QModelIndex& cell, cells) {
// if (text.length() == 0) {
// // First item
// } else if (cell.row() != currentRow) {
// // New row
// text += '\n';
// } else {
// // Next cell
// text += '\t';
// }
// currentRow = cell.row();
// text += cell.data().toString();
// }
//
qDebug() << toClip;
QApplication::clipboard()->setText(toClip);
}
}
15 changes: 14 additions & 1 deletion headers/widgets/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Includes
#include <QAbstractTableModel>
#include <QStandardItemModel>
#include <QTableWidget>
#include <QTableView>
#include <QTreeView>
#include <QWidget>
Expand Down Expand Up @@ -33,6 +34,17 @@ class ChatTable : public QAbstractTableModel {
QStringList chatTypes;
};

class SimpleTableView : public QTableView {
Q_OBJECT
public:
SimpleTableView(QWidget *parent = Q_NULLPTR);
protected:
void keyPressEvent(QKeyEvent *event);


};



class Reader : public QWidget {

Expand Down Expand Up @@ -61,7 +73,8 @@ class Reader : public QWidget {
// Variables
QString base;
QStringList headers;
QTableView *table;
// QTableView *table;
SimpleTableView *table;
QTreeView *tree;
QLabel *logTitle;
QStandardItemModel *treeModel;
Expand Down

0 comments on commit 2167659

Please sign in to comment.