2013-09-03 16:57:38 +00:00
|
|
|
#include "tableview.h"
|
|
|
|
#include "models.h"
|
|
|
|
|
|
|
|
#include <QPushButton>
|
2013-09-26 23:35:30 +00:00
|
|
|
#include <QLayout>
|
2013-09-03 16:57:38 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QSettings>
|
|
|
|
|
2013-11-12 19:57:33 +00:00
|
|
|
TableView::TableView(QWidget *parent) : QWidget(parent)
|
2013-10-03 18:54:25 +00:00
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
2013-09-03 16:57:38 +00:00
|
|
|
QFile cssFile(":table-css");
|
|
|
|
cssFile.open(QIODevice::ReadOnly);
|
|
|
|
QTextStream reader(&cssFile);
|
|
|
|
QString css = reader.readAll();
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->setStyleSheet(css);
|
2013-09-26 23:35:30 +00:00
|
|
|
/* There`s mostly a need for a Mac fix here too. */
|
|
|
|
if (qApp->style()->objectName() == "gtk+")
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->layout()->setContentsMargins(0, 9, 0, 0);
|
2013-09-26 23:35:30 +00:00
|
|
|
else
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->layout()->setContentsMargins(0, 0, 0, 0);
|
2013-09-03 16:57:38 +00:00
|
|
|
QIcon plusIcon(":plus");
|
2013-10-03 18:54:25 +00:00
|
|
|
plusBtn = new QPushButton(plusIcon, QString(), ui.groupBox);
|
2013-09-03 16:57:38 +00:00
|
|
|
plusBtn->setFlat(true);
|
|
|
|
plusBtn->setToolTip(tr("Add Cylinder"));
|
2013-09-26 23:35:30 +00:00
|
|
|
plusBtn->setIconSize(QSize(16,16));
|
2013-09-03 16:57:38 +00:00
|
|
|
connect(plusBtn, SIGNAL(clicked(bool)), this, SIGNAL(addButtonClicked()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView::~TableView()
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(objectName());
|
2013-10-03 18:54:25 +00:00
|
|
|
for (int i = 0; i < ui.tableView->model()->columnCount(); i++) {
|
|
|
|
s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i));
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::setBtnToolTip(const QString& tooltip)
|
|
|
|
{
|
|
|
|
plusBtn->setToolTip(tooltip);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::setTitle(const QString& title)
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.groupBox->setTitle(title);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::setModel(QAbstractItemModel *model){
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->setModel(model);
|
|
|
|
connect(ui.tableView, SIGNAL(clicked(QModelIndex)), model, SLOT(remove(QModelIndex)));
|
2013-09-03 16:57:38 +00:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(objectName());
|
2013-11-22 01:16:19 +00:00
|
|
|
const int columnCount = ui.tableView->model()->columnCount();
|
|
|
|
for (int i = 0; i < columnCount; i++) {
|
|
|
|
QVariant width = s.value(QString("colwidth%1").arg(i), i == CylindersModel::REMOVE ? 30 : 80 );
|
|
|
|
ui.tableView->setColumnWidth(i, width.toInt());
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
QFontMetrics metrics(defaultModelFont());
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->horizontalHeader()->setMinimumHeight(metrics.height() + 10);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::fixPlusPosition()
|
|
|
|
{
|
2013-10-03 18:54:25 +00:00
|
|
|
plusBtn->setGeometry(ui.groupBox->contentsRect().width() - 30, 2, 24,24);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to manually position the 'plus' on cylinder and weight.
|
|
|
|
void TableView::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
fixPlusPosition();
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
fixPlusPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::edit(const QModelIndex& index){
|
2013-10-03 18:54:25 +00:00
|
|
|
ui.tableView->edit(index);
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTableView *TableView::view(){
|
2013-10-03 18:54:25 +00:00
|
|
|
return ui.tableView;
|
2013-09-03 16:57:38 +00:00
|
|
|
}
|