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 @@
#include "radixtablewidget.hh"
#include <QApplication>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <limits>
......@@ -31,7 +32,7 @@ int TableModel::rowCount(const QModelIndex &parent) const
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
return 3;
}
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 (role == Qt::DisplayRole)
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
const auto &contact = contacts.at(index.row());
......@@ -48,6 +49,8 @@ QVariant TableModel::data(const QModelIndex &index, int role) const
return contact.name;
else if (index.column() == 1)
return contact.address;
else
return contact.phone;
}
return QVariant();
}
......@@ -63,14 +66,19 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation,
{
case 0:
return tr("Name");
case 1:
return tr("Address");
case 2:
return tr("Phone");
default:
return QVariant();
}
}
else if (orientation == Qt::Vertical)
{
return section;
}
return QVariant();
}
......@@ -80,7 +88,7 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
contacts.insert(position, {QString(), QString()});
contacts.insert(position, {QString(), QString(), QString()});
endInsertRows();
return true;
......@@ -110,6 +118,8 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value,
contact.name = value.toString();
else if (index.column() == 1)
contact.address = value.toString();
else if (index.column() == 2)
contact.phone = value.toString();
else
return false;
......@@ -135,11 +145,17 @@ MainWindow::MainWindow(QWidget *parent)
setGeometry(400, 250, 542, 390);
QList<Contact> contacts;
contacts << Contact{"name1", "adress 1"};
contacts << Contact{"name2", "adress 2"};
contacts << Contact{"name1", "adress 1", "11111"};
contacts << Contact{"name2", "adress 2", "22222"};
mModel = new TableModel(contacts, 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->setSeparator(',');
setCentralWidget(mTableView);
}
......
......@@ -16,6 +16,7 @@ struct Contact
{
QString name;
QString address;
QString phone;
};
class TableModel : public QAbstractTableModel
......
#include "radixwidgets/tableview.hh"
#include "radixbug/bug.hh"
#include <QApplication>
#include <QClipboard>
#include <QHeaderView>
......@@ -7,11 +9,34 @@
namespace radix
{
class TableView::PImpl
{
public:
bool allow_paste;
char separator;
PImpl();
};
TableView::PImpl::PImpl()
{
allow_paste = false;
separator = '\t';
}
TableView::TableView(QWidget *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()
{
// selected items
......@@ -57,23 +82,29 @@ void TableView::copy()
QString data;
// add column headers
for (auto col : cols)
if (horizontalHeader()->isVisible())
{
data.append('\t').append(
chmodel->headerData(col, Qt::Horizontal).toString());
for (auto col : cols)
{
data.append(chmodel->headerData(col, Qt::Horizontal).toString())
.append(p->separator);
}
data.append('\n');
}
// add cell data
for (auto row : rows)
{
// 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
for (auto col : cols)
{
data.append('\t');
// row's data
auto &rd = cells[row];
......@@ -82,19 +113,27 @@ void TableView::copy()
{
data.append(rd[col]);
}
data.append(p->separator);
}
data.append('\n');
}
// store in clipboard
QApplication::clipboard()->setText(data);
}
void TableView::paste() { radix_tagged_line("paste()"); }
void TableView::keyPressEvent(QKeyEvent *event)
{
if (event->matches(QKeySequence::Copy))
{
copy();
}
else if (event->matches(QKeySequence::Paste) && p->allow_paste)
{
paste();
}
else
{
QTableView::keyPressEvent(event);
......
......@@ -3,6 +3,8 @@
#include <QTableView>
#include <memory>
namespace radix
{
/**
......@@ -14,11 +16,22 @@ namespace radix
class TableView : public QTableView
{
Q_OBJECT
class PImpl;
std::unique_ptr<PImpl, void (*)(PImpl *)> p;
public:
TableView(QWidget *parent = 0);
char separator() const;
void setSeparator(char value);
bool allowPaste() const;
void setAllowPaste(bool value);
private:
void copy();
void paste();
public slots:
void keyPressEvent(QKeyEvent *event);
};
......
Supports Markdown
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