Commit 57350f44 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

work

parent 8bbff3b3
......@@ -5,55 +5,32 @@
#include <QTableView>
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent) : QStyledItemDelegate(parent)
{
}
QString f(int index) {
switch (index) {
case 0:
return "For Sale";
case 1:
return "Not For Sale";
case 2:
return "Hidden";
default:
return "Error";
}
}
static QString get_index_string(int);
/*
* 0: For Sale
* 1: Not For Sale
* 2: Hidden
*/
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
// TODO should the destructor actually do anything
ComboBoxItemDelegate::~ComboBoxItemDelegate() {}
// initialises initial data
QWidget* ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox* cb = new QComboBox(parent);
for (int i = 0; i < 3; i++)
cb->addItem(f(i));
cb->addItem(get_index_string(i));
return cb;
}
// Sets data when clicked
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
/*
if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
QString currentText = index.data(Qt::EditRole).toString();
int cbIndex = cb->findText(currentText);
if (cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
*/
cb->setCurrentIndex(index.data().toInt());
} else
QStyledItemDelegate::setEditorData(editor, index);
}
// sets return data
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
......@@ -62,7 +39,21 @@ void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *mod
QStyledItemDelegate::setModelData(editor, model, index);
}
// sets what's displayed when not in edit mode
QString ComboBoxItemDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
return f(value.toInt());
return get_index_string(value.toInt());
}
static QString get_index_string(int index) {
switch (index) {
case 0:
return "For Sale";
case 1:
return "Not For Sale";
case 2:
return "Hidden";
default:
return "Error";
}
}
......@@ -3,20 +3,18 @@
#include <QtCore>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QAbstractTableModel>
#include <QTableView>
#include <QSqlRelationalTableModel>
#include <QSqlRelation>
#include <QSqlRelationalDelegate>
#include <QAbstractItemView>
//include <QList>
//include <QModelIndex>
#include <QModelIndexList>
#include <QSqlError>
#include "comboboxitemdelegate.h"
// TODO maybe make it somehow possible to remove product
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
......@@ -39,28 +37,29 @@ MainWindow::MainWindow(QWidget *parent) :
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
// TODO fix column order in database
model->select();
model->setHeaderData(0, Qt::Horizontal, "id");
model->setHeaderData(1, Qt::Horizontal, "s_id");
model->setHeaderData(2, Qt::Horizontal, "b_id");
model->setHeaderData(3, Qt::Horizontal, "barcode");
model->setHeaderData(4, Qt::Horizontal, "name");
model->setHeaderData(5, Qt::Horizontal, "price");
model->setHeaderData(6, Qt::Horizontal, "status");
model->setHeaderData(1, Qt::Horizontal, "Sorting ID");
model->setHeaderData(2, Qt::Horizontal, "Systembolaget ID");
model->setHeaderData(3, Qt::Horizontal, "Sträckkod");
model->setHeaderData(4, Qt::Horizontal, "Namn");
model->setHeaderData(5, Qt::Horizontal, "Pris");
model->setHeaderData(6, Qt::Horizontal, "Till Salu?");
QTableView* view = ui->tableView;
view->setModel(model);
view->hideColumn(0); // don't show ID
// TODO fix last column
// TODO fix last column size
view->resizeColumnsToContents();
view->setEditTriggers(QAbstractItemView::AllEditTriggers);
// TODO C-s ?
ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(view);
view->setItemDelegateForColumn(6, cbid);
}
MainWindow::~MainWindow()
......@@ -79,9 +78,38 @@ void MainWindow::on_checkBox_toggled(bool checked)
void MainWindow::on_saveButton_clicked()
{
model->submitAll();
QSqlError err = QSqlDatabase::database().lastError();
qDebug() << __LINE__ << err.isValid() << err.databaseText() << err.driverText() << err.text();
}
void MainWindow::on_resetButton_clicked()
{
model->revertAll();
}
// Add new row for new product
void MainWindow::on_newItemButton_clicked()
{
QSqlQuery("INSERT INTO products (name, price) VALUES ('CHANGE ME', 0)");
model->select();
}
void MainWindow::on_deleteButton_clicked()
{
QItemSelectionModel* selection = ui->tableView->selectionModel();
if (!selection->hasSelection()) return;
//QList<QModelIndex> lst = selection->selectedRows();
QModelIndexList lst = selection->selectedRows();
foreach ( QModelIndex index, lst ) {
int row = index.row();
/*
int product_id = model->index(row, 0).data().toInt();
QSqlQuery query;
query.prepare(drop )
*/
qDebug() << __LINE__ << model->removeRow(row);
QSqlError err = QSqlDatabase::database().lastError();
qDebug() << err.isValid() << err.databaseText() << err.driverText() << err.text();
}
}
......@@ -3,6 +3,7 @@
#include <QMainWindow>
#include <QSqlTableModel>
#include <QTableView>
namespace Ui {
class MainWindow;
......@@ -23,9 +24,14 @@ private slots:
void on_resetButton_clicked();
void on_newItemButton_clicked();
void on_deleteButton_clicked();
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
};
#endif // MAINWINDOW_H
......@@ -27,9 +27,27 @@
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTableView" name="tableView">
<property name="sizeIncrement">
<size>
<width>0</width>
<height>1</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>1</height>
</size>
</property>
<property name="toolTipDuration">
<number>-1</number>
</property>
<property name="autoScroll">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
......@@ -39,61 +57,25 @@
<attribute name="horizontalHeaderHighlightSections">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderHighlightSections">
<bool>false</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Add New Product</string>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableWidget">
<property name="rowCount">
<number>1</number>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<row/>
<column>
<property name="text">
<string>sorting_id</string>
</property>
</column>
<column>
<property name="text">
<string>buy_id</string>
</property>
</column>
<column>
<property name="text">
<string>barcode</string>
</property>
</column>
<column>
<property name="text">
<string>name</string>
</property>
</column>
<column>
<property name="text">
<string>price</string>
</property>
</column>
<column>
<property name="text">
<string>status</string>
</property>
</column>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="deleteButton">
<property name="text">
<string>Delete Selected</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="newItemButton">
<property name="text">
<string>New Item</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>455</width>
<height>378</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Proudct Listings</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTableView" name="tableView">
<property name="autoScroll">
<bool>false</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="cornerButtonEnabled">
<bool>false</bool>
</property>
<attribute name="horizontalHeaderHighlightSections">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderHighlightSections">
<bool>false</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Add New Product</string>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableWidget">
<property name="rowCount">
<number>1</number>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<row/>
<column>
<property name="text">
<string>sorting_id</string>
</property>
</column>
<column>
<property name="text">
<string>buy_id</string>
</property>
</column>
<column>
<property name="text">
<string>barcode</string>
</property>
</column>
<column>
<property name="text">
<string>name</string>
</property>
</column>
<column>
<property name="text">
<string>price</string>
</property>
</column>
<column>
<property name="text">
<string>status</string>
</property>
</column>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>Show Hidden</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="resetButton">
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
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