-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
135 lines (114 loc) · 4.46 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Music");
//Initialise central widget outline
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
mainLayout->addLayout(inputsLayout);
inputsLayout->addLayout(composerLayout);
composerLayout->addWidget(composerLabel);
composerLayout->addWidget(composerInput);
inputsLayout->addLayout(albumTitleLayout);
albumTitleLayout->addWidget(albumTitleLabel);
albumTitleLayout->addWidget(albumTitleInput);
inputsLayout->addLayout(replacementCostLayout);
replacementCostLayout->addWidget(replacementCostLabel);
replacementCostLayout->addWidget(replacementCostInput);
inputsLayout->addLayout(ratingLayout);
ratingLayout->addWidget(ratingLabel);
ratingLayout->addWidget(ratingInput);
inputsLayout->addLayout(addACtionLayout);
addACtionLayout->addWidget(placeholderLabel);
addACtionLayout->addWidget(addRow);
mainLayout->addWidget(tableView);
tableView->setModel(table); //Add the table to the layoput
tableView->setItemDelegate(progressbarDelegate);
progressbarDelegate->setIndex(3);
mainLayout->addWidget(deleteRow);
//Initialise the table
QStringList labels = (QStringList() << "Composer" << "Album titles" << "Replacement cost" << "Rating");
table->setHorizontalHeaderLabels(labels);
tableView->horizontalHeader()->setStretchLastSection(true);
//Initialise input fields
composerInput->setText("");
albumTitleInput->setText("");
replacementCostInput->setValue(0.00);
replacementCostInput->setMaximum(1000000.00);
replacementCostInput->setMinimum(0);
ratingInput->setValue(0);
ratingInput->setMaximum(100);
ratingInput->setMinimum(0);
rowCount = 0;
selectedRowIndex = -1;
//HeaderView used to order rows
QHeaderView *headerView = tableView->horizontalHeader();
//Connect signals and slots
connect(addRow, &QPushButton::clicked, this, &MainWindow::addClicked);
connect(headerView, &QHeaderView::sectionClicked, this, &MainWindow::orderClicked);
connect(tableView, &QTableView::clicked, this, &MainWindow::selectedRow);
connect(deleteRow, &QPushButton::released, this, &MainWindow::deleteClicked);
}
MainWindow::~MainWindow()
{
}
void MainWindow::orderClicked(int logicalIndex)
{
Qt::SortOrder sortOrder = tableView->horizontalHeader()->sortIndicatorOrder();
if(sortOrder == Qt::AscendingOrder)
{
sortOrder = Qt::DescendingOrder;
}
else
{
sortOrder = Qt::AscendingOrder;
}
tableView->horizontalHeader()->setSortIndicator(logicalIndex, sortOrder);
tableView->sortByColumn(logicalIndex, sortOrder);
}
void MainWindow::clearInputs()
{
composerInput->setText("");
albumTitleInput->setText("");
replacementCostInput->setValue(0.00);
ratingInput->setValue(0);
}
void MainWindow::addClicked()
{
//Convert inputs into QStandardItem
QStandardItem *composerItem = new QStandardItem;
composerItem->setText(composerInput->text());
table->setItem(rowCount,0,composerItem);
QStandardItem *albumTitleItem = new QStandardItem;
albumTitleItem->setText(albumTitleInput->text());
table->setItem(rowCount,1,albumTitleItem);
QStandardItem *replacementCostItem = new QStandardItem;
QString replacementCostValue;
replacementCostValue = QString::number(replacementCostInput->value(), 'f', 2);
replacementCostItem->setText(replacementCostValue);
table->setItem(rowCount,2,replacementCostItem);
if(replacementCostInput->value() > 200)
{
QBrush blush(Qt::red);
composerItem->setBackground(blush);
albumTitleItem->setBackground(blush);
replacementCostItem->setBackground(blush);
}
//Delegate used to paint progressbar in desired column
table->setData(table->index(rowCount,3),ratingInput->value(), Qt::DisplayRole);
/*Remember items for deletion? Add them to a QList<T>*/
rowCount++; //Increment the number of rows
this->clearInputs(); //Clear the UI
}
void MainWindow::selectedRow(const QModelIndex &index)
{
selectedRowIndex = index.row();
}
void MainWindow::deleteClicked()
{
//Can call clear() to remove all items from the model
//QStandardItemModel->removeRow(Index)
qDebug() << selectedRowIndex;
table->removeRow(selectedRowIndex);
}