Commit 60a3d300 authored by Hugo Hörnquist's avatar Hugo Hörnquist

Removed note system

The note system was a mistake to begin with, and nobody used it,
so it's gone now.

This commit should contain all parts of it, if someone wants to
look at it again in the future.

Old copies of the database could still have the table for notes,
this should probably be ok to drop.
parent dd80f5f0
......@@ -19,8 +19,6 @@ SOURCES += main.cpp\
moneydelegate.cpp \
texteditdelegate.cpp \
disablingmodel.cpp \
qsqltreemodel.cpp \
qsqltreeitem.cpp \
qintornullvalidator.cpp \
maybevalidatordelegate.cpp
......@@ -30,8 +28,6 @@ HEADERS += mainwindow.h \
moneydelegate.h \
texteditdelegate.h \
disablingmodel.h \
qsqltreemodel.h \
qsqltreeitem.h \
qintornullvalidator.h \
maybevalidatordelegate.h
......
......@@ -13,8 +13,6 @@
#include <QSqlRelationalDelegate>
#include <QHeaderView>
#include <QSpinBox>
#include <QMessageBox>
#include <QFileInfo>
......@@ -29,8 +27,6 @@
#include "qintornullvalidator.h"
#include "qsqltreemodel.h"
#include "serial.h"
#define CASH_ACCOUNT "Cash Drawer"
......@@ -53,8 +49,6 @@
// - search auto clear?
// - sugested buy from systemet (this is replaced by reading from done acquisitions)
// TODO can't edit note, only replace content
// TODO money diff doesn't update when cash drawer changes elsewhere in program
// it however still inserts the correct data into the db
// This binds into most strings not auto updating, and some more needing manual updates
......@@ -244,30 +238,6 @@ MainWindow::MainWindow(QWidget *parent) :
// ==================================================
QList<int> notesDisabledColumns;
notesDisabledColumns << 0 << 3;
DisablingModel* notesModel = new DisablingModel(notesDisabledColumns);
notesModel->setTable("notes");
notesModel->setHeaderData(0, Qt::Horizontal, "id");
notesModel->setHeaderData(1, Qt::Horizontal, "Produkt");
notesModel->setHeaderData(2, Qt::Horizontal, "Notering");
notesModel->setHeaderData(3, Qt::Horizontal, "Skapad");
notesModel->setSort(3, Qt::AscendingOrder);
notesModel->setRelation(1, QSqlRelation("products", "id", "name"));
notesModel->setEditStrategy(QSqlTableModel::OnFieldChange);
notesModel->select();
QSqlTreeModel* qstm = new QSqlTreeModel;
ui->notesTreeView->setModel(qstm);
ui->notesTreeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->notesTreeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->notesTreeView->setSelectionBehavior (QAbstractItemView::SelectRows);
//ui->notesTreeView->setItemDelegateForColumn(0, new TextEditDelegate);
ui->notesTreeView->hideColumn(2);
setNoteAddSelect("");
// ==================================================
clear_stockDiffTemp();
QList<int> inventoryCheckDisabled;
......@@ -741,70 +711,11 @@ void MainWindow::updateCurrentDrainageStatus()
}
}
void MainWindow::on_deleteNoteButton_clicked()
{
QSqlTreeItem* parent = ((QSqlTreeModel*) ui->notesTreeView->model())
->getItem(ui->notesTreeView->currentIndex());
QVariant id = parent->data(2);
QSqlQuery query;
query.prepare ("DELETE FROM notes WHERE id = :id");
query.bindValue(":id", id);
if (!query.exec())
qDebug() << query.lastError();
((QSqlTreeModel*) ui->notesTreeView->model())->requery();
}
void MainWindow::on_stockRefreshButton_clicked()
{
((QSqlRelationalTableModel*) ui->stockView->model())->select();
}
void MainWindow::on_noteAddSearch_textChanged(const QString &search)
{
setNoteAddSelect(search);
}
void MainWindow::setNoteAddSelect(const QString &search)
{
ui->noteAddSelect->clear();
ui->noteAddSelect->addItem("No Item", 0);
QSqlQuery query;
query.prepare("SELECT id, name "
"FROM products "
"WHERE sale_status IN (0, 1) "
"AND name like :search "
" OR bar_code = :exact_search");
query.bindValue(":search", "%" + search + "%");
query.bindValue(":exact_search", search);
if (!query.exec())
qDebug() << query.lastError();
while (query.next()) {
ui->noteAddSelect->addItem(query.value(1).toString(), query.value(0));
}
if (ui->noteAddSelect->count() > 1) {
ui->noteAddSelect->setCurrentIndex(1);
}
}
void MainWindow::on_noteAddAdd_clicked()
{
QVariant id = ui->noteAddSelect->currentData();
QSqlQuery query;
if (id == QVariant(0))
query.prepare("INSERT INTO notes DEFAULT VALUES");
else {
query.prepare("INSERT INTO notes (product_id) VALUES (:id)");
query.bindValue(":id", id);
}
query.exec();
((QSqlTreeModel*) ui->notesTreeView->model())->requery();
}
void MainWindow::on_diffSubmit_clicked()
{
QString str = ui->diffDrawer->text();
......
......@@ -61,16 +61,10 @@ private slots:
void on_updateCurrentDrainageButton_clicked();
void on_deleteNoteButton_clicked();
void on_productModel_dataChanged(const QModelIndex& topLeft, const QModelIndex& bottmRight, const QVector<int>& roles);
void on_stockRefreshButton_clicked();
void on_noteAddSearch_textChanged(const QString &arg1);
void on_noteAddAdd_clicked();
void on_diffSubmit_clicked();
void on_diffDrawer_textChanged(const QString &arg1);
......@@ -90,7 +84,6 @@ private:
char* port_name;
void setNoteAddSelect(const QString& search);
void updateMoneyString();
void setMoneyDiffLabels(int);
int setMoneyDiffLabels();
......
......@@ -270,64 +270,6 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="noteTab">
<attribute name="title">
<string>Noteringar</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QTreeView" name="notesTreeView">
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<spacer name="horizontalSpacer_4">
<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="QLineEdit" name="noteAddSearch">
<property name="placeholderText">
<string>Search...</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="noteAddSelect"/>
</item>
<item>
<widget class="QPushButton" name="noteAddAdd">
<property name="text">
<string>New Product Note</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteNoteButton">
<property name="text">
<string>Delete Note</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Invertering</string>
......
#include "qsqltreeitem.h"
// http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
QSqlTreeItem::QSqlTreeItem(const QList<QVariant>& data, QSqlTreeItem* parentItem)
{
m_parentItem = parentItem;
m_itemData = data;
}
QSqlTreeItem::~QSqlTreeItem()
{
qDeleteAll(m_childItems);
}
void QSqlTreeItem::appendChild(QSqlTreeItem *child)
{
m_childItems.append(child);
}
QSqlTreeItem* QSqlTreeItem::child(int row)
{
return m_childItems.value(row);
}
int QSqlTreeItem::childCount() const
{
return m_childItems.count();
}
/*
* returns the item's location within its parent's list of items
*
* TODO how does this work
*/
int QSqlTreeItem::row() const
{
if (m_parentItem)
return m_parentItem->m_childItems.indexOf(const_cast <QSqlTreeItem*>(this));
return 0;
}
int QSqlTreeItem::columnCount() const
{
return m_itemData.count();
}
QVariant QSqlTreeItem::data (int column) const
{
return m_itemData.value (column);
}
QSqlTreeItem* QSqlTreeItem::parentItem()
{
return m_parentItem;
}
bool QSqlTreeItem::insertChildren(int position, int count, int columns)
{
if (position < 0 || position > m_childItems.size())
return false;
//this->appendChild();
for (int row = 0; row < count; row++) {
QList<QVariant> data;
for (int i = 0; i < columns; i++) {
data << "";
}
QSqlTreeItem* item = new QSqlTreeItem(data, this);
m_childItems.insert(position, item);
}
return true;
}
bool QSqlTreeItem::removeChildren(int position, int count)
{
if (position < 0 || position > m_childItems.size())
return false;
for (int row = 0; row < count; row++)
delete m_childItems.takeAt(position);
return true;
}
bool QSqlTreeItem::setData(int column, const QVariant &value)
{
if (column < 0 || column >= m_itemData.size())
return false;
m_itemData[column] = value;
return true;
}
#ifndef QSQLTREEITEM_H
#define QSQLTREEITEM_H
#include <QtCore>
class QSqlTreeItem
{
public:
explicit QSqlTreeItem(const QList<QVariant>& data, QSqlTreeItem* parentItem = 0);
~QSqlTreeItem();
void appendChild (QSqlTreeItem* child);
QSqlTreeItem* child (int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
QSqlTreeItem* parentItem();
bool insertChildren(int position, int count, int columns);
//bool insertColumns(int position, int columns);
bool removeChildren(int position, int count);
//bool removeColumns(int position, int columns);
bool setData(int column, const QVariant& value);
private:
QList<QSqlTreeItem*> m_childItems;
QList<QVariant> m_itemData;
QSqlTreeItem* m_parentItem;
};
#endif // QSQLTREEITEM_H
#include "qsqltreemodel.h"
#include <QSqlQuery>
#include <QSqlError>
QSqlTreeModel::QSqlTreeModel(QObject* parent)
: QAbstractItemModel(parent)
{
rootItem = 0;
requery();
}
QSqlTreeModel::~QSqlTreeModel()
{
delete rootItem;
}
QModelIndex QSqlTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
QSqlTreeItem* parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<QSqlTreeItem*>(parent.internalPointer());
QSqlTreeItem* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex QSqlTreeModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
QSqlTreeItem* childItem = static_cast<QSqlTreeItem*>(child.internalPointer());
QSqlTreeItem* parentItem = childItem->parentItem();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int QSqlTreeModel::rowCount (const QModelIndex &parent) const
{
QSqlTreeItem* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<QSqlTreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int QSqlTreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<QSqlTreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
QVariant QSqlTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
QSqlTreeItem* item = static_cast<QSqlTreeItem*>(index.internalPointer());
return item->data(index.column());
}
Qt::ItemFlags QSqlTreeModel::flags (const QModelIndex &index) const
{
if (!index.isValid())
return 0;
QSqlTreeItem* item = getItem(index);
if (item->parentItem() == rootItem || index.column() != 0)
return QAbstractItemModel::flags(index);
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
QVariant QSqlTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
/*
* Clear model and insert new current data
*/
void QSqlTreeModel::requery()
{
if (rootItem != 0)
delete rootItem;
QList<QVariant> rootData;
// TODO possibly figure out way to not have to readd the headers
rootData << "Product/note" << "Date" << "id (hidden)";
rootItem = new QSqlTreeItem(rootData);
setupModelData(rootItem);
endResetModel();
}
void QSqlTreeModel::setupModelData(QSqlTreeItem *parent)
{
// TODO sorting
QSqlQuery query(
"SELECT ifnull(p.id, 0), ifnull(p.name, 'No Name') "
"FROM notes n "
"LEFT JOIN products p "
"ON n.product_id = p.id "
"GROUP BY p.id");
QSqlTreeItem* root = parent;
QSqlTreeItem* currentParent = root;
while (query.next()) {
QList<QVariant> lst;
lst << query.value(1) << "" << "";
currentParent = new QSqlTreeItem(lst, root);
root->appendChild(currentParent);
QSqlQuery inner;
inner.prepare("SELECT n.note, n.created, n.id "
"FROM notes n "
"LEFT JOIN products p "
"ON n.product_id = p.id "
"WHERE ifnull(p.id, 0) = :id ");
inner.bindValue(":id", query.value(0));
if (!inner.exec())
qDebug() << inner.lastError();
while (inner.next()) {
QList<QVariant> lt;
lt << inner.value(0) << inner.value(1) << inner.value(2);
currentParent->appendChild(new QSqlTreeItem(lt, currentParent));
}
}
}
bool QSqlTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
QSqlTreeItem* item = getItem(index);
QSqlTreeItem* parent = item->parentItem();
// This doesn't even have edit flags
if (parent == rootItem)
return false;
bool result = item->setData(index.column(), value);
if (result) {
QSqlQuery query;
query.prepare("update notes set note = :note where id = :id");
query.bindValue(":note", value);
query.bindValue(":id", item->data(2));
if (!query.exec()) {
qDebug() << query.lastError();
return false;
}
emit dataChanged(index, index);
}
return result;
}
// TODO better
bool QSqlTreeModel::insertRow(int row, const QModelIndex &parent)
{
getItem(parent)->insertChildren(row, 1, 1);
return true;
}
// TODO better
bool QSqlTreeModel::removeRow(int row, const QModelIndex &parent)
{
getItem(parent)->removeChildren(row, 1);
return true;
}
QSqlTreeItem* QSqlTreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
QSqlTreeItem* item = static_cast<QSqlTreeItem*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
#ifndef QSQLTREEMODEL_H
#define QSQLTREEMODEL_H
#include <QAbstractItemModel>
#include "qsqltreeitem.h"
class QSqlTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit QSqlTreeModel(QObject* parent = 0);
~QSqlTreeModel();
QVariant data (const QModelIndex &index, int role) const;
Qt::ItemFlags flags (const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index (int row, int column, const QModelIndex &parent) const;
QModelIndex parent (const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
//bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role);
//bool insertColumn(int column, const QModelIndex &parent);
//bool insertColumns(int column, int count, const QModelIndex &parent);
bool insertRow(int row, const QModelIndex &parent);
//bool insertRows(int row, int count, const QModelIndex &parent);
bool removeRow(int row, const QModelIndex &parent);
//bool removeRows(int row, int count, const QModelIndex &parent);
QSqlTreeItem* getItem(const QModelIndex& index) const;
void requery();
private:
void setupModelData (QSqlTreeItem* parent);
QSqlTreeItem* rootItem;
};
#endif // QSQLTREEMODEL_H
......@@ -35,15 +35,6 @@ CREATE TABLE log (
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TABLE notes (
id INTEGER PRIMARY KEY NOT NULL,
product_id INTEGER,
note TEXT DEFAULT "",
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TABLE money ( -- variable table
id INTEGER PRIMARY KEY NOT NULL,
name TEXT UNIQUE,
......
......@@ -76,10 +76,6 @@ via different columns. Only *active* is used.
| time | TIMESTAMP | CURRENT_TIMESTAMP | not null | |
| price | INTEGER | | NOT NULL | |
*** Notes
Legend says there once was a type of file called /text file/,
The historians know little about these magical items.
*** Money
This is a state table, so it's one of the few places with mutable
data.
......
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