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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
Copyright (c) 2006-2009, Tom Thielicke IT Solutions
SPDX-License-Identifier: GPL-2.0-only
*/
/****************************************************************
**
** Implementation of the LessonTableSql class
** File name: lessontablesql.h
**
****************************************************************/
#include <QAbstractItemView>
#include <QChar>
#include <QColor>
#include <QCoreApplication>
#include <QDateTime>
#include <QFont>
#include <QHBoxLayout>
#include <QItemSelectionModel>
#include <QList>
#include <QObject>
#include <QPen>
#include <QString>
#include <QTextCharFormat>
#include <QTextCursor>
#include <QTextEdit>
#include <QTextFrame>
#include <QTextFrameFormat>
#include <QTextTable>
#include <QTextTableFormat>
#include <QVBoxLayout>
#include <cmath>
#include "lessontablesql.h"
LessonSqlModel::LessonSqlModel(int row, int type, QWidget* parent)
: QSqlQueryModel(parent)
, lastIdInserted(row)
, lastTypeInserted(type)
, parentWidget(parent)
{
}
QVariant LessonSqlModel::data(const QModelIndex& index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
QDateTime timeStamp;
static int coloredRow = -1;
QString lessonName;
if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 0) {
// Expand lesson name
lessonName = value.toString();
return lessonName; //.prepend(tr("Uebungslektion "));
}
if (index.column() == 1) {
// Convert time stamp into a readable format
timeStamp
= QDateTime::fromString(value.toString(), "yyyyMMddhhmmss");
return timeStamp.toString(QLocale::system().dateTimeFormat(
QLocale::FormatType::ShortFormat));
}
if (index.column() == 2) {
int timeSec = value.toInt();
// Show time length in seconds or minutes
if (timeSec < 60) {
return tr("%L1 s").arg(timeSec);
}
double timeMin = floor((timeSec / 60.0) / 0.1 + 0.5) * 0.1;
return tr("%L1 min").arg(timeMin, 0, 'f', 1);
}
if (index.column() == 5) {
// Rate
return tr("%L1 %").arg(value.toDouble(), 0, 'f', 1);
}
if (index.column() == 6) {
// There is never grade smaller than zero
return QString("%1").arg(value.toInt());
}
if (index.column() == 7) {
double lessonGrade = value.toDouble();
// There is never grade smaller than zero
if (lessonGrade < 0) {
lessonGrade = 0;
}
return tr("%n point(s)", "", static_cast<int>(lessonGrade));
}
if (index.column() == 8 && value.toInt() == lastIdInserted) {
// Current row has to be colored
coloredRow = index.row();
return QString::number(coloredRow);
}
if (lastIdInserted == 0) {
// No row is colored
coloredRow = -1;
}
}
if (role == Qt::FontRole && (index.column() == 0 || index.column() == 7)) {
// Show the lesson number bold
QFont font;
font = parentWidget->font();
font.setBold(true);
return QVariant::fromValue(font);
}
return value;
}
LessonTableSql::LessonTableSql(int row, int type, QWidget* parent)
: QWidget(parent)
, comboFilter(new QComboBox())
, model(new LessonSqlModel(row, type, this))
, view(new QTableView())
, headerview(view->horizontalHeader())
, previousColumnIndex(-1)
, whereClausel("")
{
// Create filter headline
labelFilter = new QLabel(tr("Show: "));
comboFilter->insertItem(0, tr("All Lessons"));
comboFilter->insertItem(1, tr("Training Lessons"));
comboFilter->insertItem(2, tr("Open Lessons"));
comboFilter->insertItem(3, tr("Own Lessons"));
view->setModel(model);
// User should not be able to select a row
view->setSelectionMode(QAbstractItemView::NoSelection);
// Hide the lesson id column
headerview->setStretchLastSection(true);
headerview->setSectionResizeMode(QHeaderView::Interactive);
headerview->setSortIndicatorShown(true);
// Set the sql query (every lesson, it's properties and rating)
sortColumn(-1);
// Set a horizonal layout
QHBoxLayout* filterLayout = new QHBoxLayout;
filterLayout->addStretch(1);
filterLayout->addWidget(labelFilter);
filterLayout->addWidget(comboFilter);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addLayout(filterLayout);
mainLayout->addWidget(view);
// Pass layout to parent widget (this)
this->setLayout(mainLayout);
connect(headerview, &QHeaderView::sectionClicked, this,
&LessonTableSql::sortColumn);
connect(comboFilter, &QComboBox::activated, this,
&LessonTableSql::changeFilter);
}
void LessonTableSql::sortColumn(int columnindex)
{
// Select columnname from columnindex
switch (columnindex) {
case 0:
columnName = "user_lesson_lesson";
break;
case 1:
default:
columnName = "user_lesson_timestamp";
columnindex = 1;
break;
case 2:
columnName = "user_lesson_timelen";
break;
case 3:
columnName = "user_lesson_tokenlen";
break;
case 4:
columnName = "user_lesson_errornum";
break;
case 5:
columnName = "user_lesson_rate";
break;
case 6:
columnName = "user_lesson_cpm";
break;
case 7:
columnName = "user_lesson_grade";
break;
}
if (previousColumnIndex != columnindex) {
isDesc = true;
headerview->setSortIndicator(columnindex, Qt::DescendingOrder);
} else {
isDesc = (headerview->sortIndicatorOrder() != Qt::AscendingOrder);
}
previousColumnIndex = columnindex;
if (columnindex != -1) {
model->lastIdInserted = 0;
}
// Call new query
setQueryOrder(columnName, isDesc);
}
void LessonTableSql::changeFilter(int rowindex)
{
// Select columnname from columnindex
switch (rowindex) {
case 0:
whereClausel = "";
break;
case 1:
default:
whereClausel = "WHERE user_lesson_type = 0 ";
break;
case 2:
whereClausel = "WHERE user_lesson_type = 1 ";
break;
case 3:
whereClausel = "WHERE user_lesson_type = 2 ";
break;
}
model->lastIdInserted = 0;
// Call new query
setQueryOrder(columnName, isDesc);
}
void LessonTableSql::setQueryOrder(QString columnname, bool isdesc)
{
QString descText = isdesc ? " DESC" : " ASC";
model->clear();
// Set the sql query (every lesson, it's properties and rating)
model->setQuery(
"SELECT user_lesson_name, user_lesson_timestamp, "
"user_lesson_timelen, user_lesson_tokenlen, "
"user_lesson_errornum, "
"((user_lesson_errornum * 100.0) / "
" user_lesson_strokesnum) AS user_lesson_rate, "
"(user_lesson_strokesnum / "
"(user_lesson_timelen / 60.0)) AS user_lesson_cpm, "
"(((user_lesson_strokesnum - (20.0 * user_lesson_errornum)) / "
"(user_lesson_timelen / 60.0)) * 0.4) AS user_lesson_grade, "
"user_lesson_id FROM user_lesson_list "
+ whereClausel + "ORDER BY " + columnname + descText + ";");
view->setColumnHidden(8, true);
setModelHeader();
view->resizeColumnsToContents();
}
void LessonTableSql::setModelHeader()
{
// Column headers (see sql query)
model->setHeaderData(0, Qt::Horizontal, tr("Lesson"));
model->setHeaderData(1, Qt::Horizontal, tr("Time"));
model->setHeaderData(2, Qt::Horizontal, tr("Duration"));
model->setHeaderData(3, Qt::Horizontal, tr("Characters"));
model->setHeaderData(4, Qt::Horizontal, tr("Errors"));
model->setHeaderData(5, Qt::Horizontal, tr("Rate"));
model->setHeaderData(6, Qt::Horizontal, tr("Cpm"));
model->setHeaderData(7, Qt::Horizontal, tr("Score"));
model->setHeaderData(0, Qt::Horizontal,
tr("This column shows the names\n"
"of completed lessons"),
Qt::ToolTipRole);
model->setHeaderData(
1, Qt::Horizontal, tr("Start time of the lesson"), Qt::ToolTipRole);
model->setHeaderData(
2, Qt::Horizontal, tr("Total duration of the lesson"), Qt::ToolTipRole);
model->setHeaderData(3, Qt::Horizontal, tr("Number of characters dictated"),
Qt::ToolTipRole);
model->setHeaderData(
4, Qt::Horizontal, tr("Number of typing errors"), Qt::ToolTipRole);
model->setHeaderData(5, Qt::Horizontal,
tr("The error rate is calculated as follows:\n"
"Errors / Characters\n"
"The lower the error rate the better!"),
Qt::ToolTipRole);
model->setHeaderData(6, Qt::Horizontal,
tr("\"Cpm\" indicates how many characters per minute\n"
"were entered on average"),
Qt::ToolTipRole);
model->setHeaderData(7, Qt::Horizontal,
tr("The score is calculated as follows:\n"
"((Characters - (20 x Errors)) / Duration in minutes) x 0.4\n"
"\n"
"Note that slow typing without errors results in a\n"
"better ranking, than fast typing with several errors!"),
Qt::ToolTipRole);
}
|