Commit b104d235 authored by Jordan P. Lefebvre's avatar Jordan P. Lefebvre
Browse files

Added tableview separator option. Starting allow paste option.

parent 9643898b
Pipeline #13848 failed with stages
in 6 minutes and 18 seconds
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "radixtablewidget.hh" #include "radixtablewidget.hh"
#include <QApplication> #include <QApplication>
#include <QGridLayout> #include <QGridLayout>
#include <QHeaderView>
#include <QLabel> #include <QLabel>
#include <limits> #include <limits>
...@@ -31,7 +32,7 @@ int TableModel::rowCount(const QModelIndex &parent) const ...@@ -31,7 +32,7 @@ int TableModel::rowCount(const QModelIndex &parent) const
int TableModel::columnCount(const QModelIndex &parent) const int TableModel::columnCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
return 2; return 3;
} }
QVariant TableModel::data(const QModelIndex &index, int role) const QVariant TableModel::data(const QModelIndex &index, int role) const
...@@ -40,7 +41,7 @@ QVariant TableModel::data(const QModelIndex &index, int role) const ...@@ -40,7 +41,7 @@ QVariant TableModel::data(const QModelIndex &index, int role) const
if (index.row() >= contacts.size() || index.row() < 0) return QVariant(); if (index.row() >= contacts.size() || index.row() < 0) return QVariant();
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole || role == Qt::EditRole)
{ {
const auto &contact = contacts.at(index.row()); const auto &contact = contacts.at(index.row());
...@@ -48,6 +49,8 @@ QVariant TableModel::data(const QModelIndex &index, int role) const ...@@ -48,6 +49,8 @@ QVariant TableModel::data(const QModelIndex &index, int role) const
return contact.name; return contact.name;
else if (index.column() == 1) else if (index.column() == 1)
return contact.address; return contact.address;
else
return contact.phone;
} }
return QVariant(); return QVariant();
} }
...@@ -63,14 +66,19 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, ...@@ -63,14 +66,19 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation,
{ {
case 0: case 0:
return tr("Name"); return tr("Name");
case 1: case 1:
return tr("Address"); return tr("Address");
case 2:
return tr("Phone");
default: default:
return QVariant(); return QVariant();
} }
} }
else if (orientation == Qt::Vertical)
{
return section;
}
return QVariant(); return QVariant();
} }
...@@ -80,7 +88,7 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &index) ...@@ -80,7 +88,7 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
beginInsertRows(QModelIndex(), position, position + rows - 1); beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) for (int row = 0; row < rows; ++row)
contacts.insert(position, {QString(), QString()}); contacts.insert(position, {QString(), QString(), QString()});
endInsertRows(); endInsertRows();
return true; return true;
...@@ -110,6 +118,8 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value, ...@@ -110,6 +118,8 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value,
contact.name = value.toString(); contact.name = value.toString();
else if (index.column() == 1) else if (index.column() == 1)
contact.address = value.toString(); contact.address = value.toString();
else if (index.column() == 2)
contact.phone = value.toString();
else else
return false; return false;
...@@ -135,11 +145,17 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -135,11 +145,17 @@ MainWindow::MainWindow(QWidget *parent)
setGeometry(400, 250, 542, 390); setGeometry(400, 250, 542, 390);
QList<Contact> contacts; QList<Contact> contacts;
contacts << Contact{"name1", "adress 1"}; contacts << Contact{"name1", "adress 1", "11111"};
contacts << Contact{"name2", "adress 2"}; contacts << Contact{"name2", "adress 2", "22222"};
mModel = new TableModel(contacts, this); mModel = new TableModel(contacts, this);
mTableView = new radix::TableView(this); mTableView = new radix::TableView(this);
mTableView->setAllowPaste(true);
//
// Comment these or Uncomment these to see effect on copy
// mTableView->verticalHeader()->setVisible(false);
// mTableView->horizontalHeader()->setVisible(false);
mTableView->setModel(mModel); mTableView->setModel(mModel);
mTableView->setSeparator(',');
setCentralWidget(mTableView); setCentralWidget(mTableView);
} }
......
...@@ -16,6 +16,7 @@ struct Contact ...@@ -16,6 +16,7 @@ struct Contact
{ {
QString name; QString name;
QString address; QString address;
QString phone;
}; };
class TableModel : public QAbstractTableModel class TableModel : public QAbstractTableModel
......
#include "radixwidgets/tableview.hh" #include "radixwidgets/tableview.hh"
#include "radixbug/bug.hh"
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QHeaderView> #include <QHeaderView>
...@@ -7,11 +9,34 @@ ...@@ -7,11 +9,34 @@
namespace radix namespace radix
{ {
class TableView::PImpl
{
public:
bool allow_paste;
char separator;
PImpl();
};
TableView::PImpl::PImpl()
{
allow_paste = false;
separator = '\t';
}
TableView::TableView(QWidget *parent) TableView::TableView(QWidget *parent)
: QTableView(parent) : QTableView(parent)
, p(new PImpl(), [](PImpl *impl) { delete impl; })
{ {
} }
char TableView::separator() const { return p->separator; }
void TableView::setSeparator(char value) { p->separator = value; }
bool TableView::allowPaste() const { return p->allow_paste; }
void TableView::setAllowPaste(bool value) { p->allow_paste = value; }
void TableView::copy() void TableView::copy()
{ {
// selected items // selected items
...@@ -57,23 +82,29 @@ void TableView::copy() ...@@ -57,23 +82,29 @@ void TableView::copy()
QString data; QString data;
// add column headers // add column headers
for (auto col : cols) if (horizontalHeader()->isVisible())
{ {
data.append('\t').append( for (auto col : cols)
chmodel->headerData(col, Qt::Horizontal).toString()); {
data.append(chmodel->headerData(col, Qt::Horizontal).toString())
.append(p->separator);
}
data.append('\n');
} }
// add cell data // add cell data
for (auto row : rows) for (auto row : rows)
{ {
// add row header // add row header
data.append('\n').append(rhmodel->headerData(row, Qt::Vertical).toString()); if (verticalHeader()->isVisible())
{
data.append(rhmodel->headerData(row, Qt::Vertical).toString())
.append(p->separator);
}
// add row's cells // add row's cells
for (auto col : cols) for (auto col : cols)
{ {
data.append('\t');
// row's data // row's data
auto &rd = cells[row]; auto &rd = cells[row];
...@@ -82,19 +113,27 @@ void TableView::copy() ...@@ -82,19 +113,27 @@ void TableView::copy()
{ {
data.append(rd[col]); data.append(rd[col]);
} }
data.append(p->separator);
} }
data.append('\n');
} }
// store in clipboard // store in clipboard
QApplication::clipboard()->setText(data); QApplication::clipboard()->setText(data);
} }
void TableView::paste() { radix_tagged_line("paste()"); }
void TableView::keyPressEvent(QKeyEvent *event) void TableView::keyPressEvent(QKeyEvent *event)
{ {
if (event->matches(QKeySequence::Copy)) if (event->matches(QKeySequence::Copy))
{ {
copy(); copy();
} }
else if (event->matches(QKeySequence::Paste) && p->allow_paste)
{
paste();
}
else else
{ {
QTableView::keyPressEvent(event); QTableView::keyPressEvent(event);
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <QTableView> #include <QTableView>
#include <memory>
namespace radix namespace radix
{ {
/** /**
...@@ -14,11 +16,22 @@ namespace radix ...@@ -14,11 +16,22 @@ namespace radix
class TableView : public QTableView class TableView : public QTableView
{ {
Q_OBJECT Q_OBJECT
class PImpl;
std::unique_ptr<PImpl, void (*)(PImpl *)> p;
public: public:
TableView(QWidget *parent = 0); TableView(QWidget *parent = 0);
char separator() const;
void setSeparator(char value);
bool allowPaste() const;
void setAllowPaste(bool value);
private: private:
void copy(); void copy();
void paste();
public slots: public slots:
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment