Commit 8bbff3b3 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

basicaly all work

parents
#include "comboboxitemdelegate.h"
#include <QComboBox>
#include <QtCore>
#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";
}
}
/*
* 0: For Sale
* 1: Not For Sale
* 2: Hidden
*/
ComboBoxItemDelegate::~ComboBoxItemDelegate() {}
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));
return cb;
}
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);
}
*/
QStyledItemDelegate::setEditorData(editor, index);
}
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
model->setData(index, cb->currentIndex(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
QString ComboBoxItemDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
return f(value.toInt());
}
#ifndef COMBOBOXITEMDELEGATE_H
#define COMBOBOXITEMDELEGATE_H
#include <QStyledItemDelegate>
class ComboBoxItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxItemDelegate(QObject* parent = 0);
~ComboBoxItemDelegate();
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const;
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
virtual QString displayText(const QVariant &value, const QLocale &locale) const;
};
#endif // COMBOBOXITEMDELEGATE_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#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 "comboboxitemdelegate.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("stupan.db");
if (!db.open()) qDebug() << "database connection failed";
QSqlQuery("PRAGMA FOREIGN_KEYS = ON");
// TODO indicate graphicaly that unsaved changes exist
// possibly also (dis|en)able the buttons
model = new QSqlTableModel;
model->setTable("products");
model->setFilter("sale_status != 2");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
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");
QTableView* view = ui->tableView;
view->setModel(model);
view->hideColumn(0); // don't show ID
// TODO fix last column
view->resizeColumnsToContents();
view->setEditTriggers(QAbstractItemView::AllEditTriggers);
ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(view);
view->setItemDelegateForColumn(6, cbid);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_checkBox_toggled(bool checked)
{
if(checked)
model->setFilter("");
else
model->setFilter("sale_status != 2");
}
void MainWindow::on_saveButton_clicked()
{
model->submitAll();
}
void MainWindow::on_resetButton_clicked()
{
model->revertAll();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlTableModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_checkBox_toggled(bool checked);
void on_saveButton_clicked();
void on_resetButton_clicked();
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
};
#endif // MAINWINDOW_H
<?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>
<?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