2018-08-12 20:51:57 +02:00
|
|
|
/*
|
|
|
|
Speed Climbing Stopwatch - Simple Stopwatch for Climbers
|
|
|
|
Copyright (C) 2018 Itsblue Development - Dorian Zeder
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-28 23:05:26 +02:00
|
|
|
#include "headers/sqlprofilemodel.h"
|
2018-07-22 16:47:55 +02:00
|
|
|
|
|
|
|
static void createTable()
|
|
|
|
{
|
|
|
|
if (QSqlDatabase::database().tables().contains(QStringLiteral("Contacts"))) {
|
|
|
|
// The table already exists; we don't need to do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
|
|
|
//creat eth etable to store the profiles
|
|
|
|
if (!query.exec(
|
|
|
|
"CREATE TABLE IF NOT EXISTS `profiles` ( "
|
|
|
|
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
|
|
|
|
" `name` TEXT NOT NULL "
|
|
|
|
" );")) {
|
|
|
|
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//create the table to store the times
|
|
|
|
if (!query.exec(
|
|
|
|
"CREATE TABLE IF NOT EXISTS `times` ("
|
|
|
|
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
|
|
|
|
" `profileid` INTEGER NOT NULL,"
|
2018-08-02 12:50:55 +02:00
|
|
|
" `time` INTEGER NOT NULL, "
|
|
|
|
" `timestamp` INTEGER NOT NULL"
|
2018-07-22 16:47:55 +02:00
|
|
|
" );")) {
|
|
|
|
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SqlProfileModel::SqlProfileModel(QObject *parent) : QSqlTableModel(parent)
|
|
|
|
{
|
|
|
|
qDebug("ProfileModel constructor");
|
|
|
|
createTable();
|
|
|
|
setTable("profiles");
|
2018-08-03 15:18:23 +02:00
|
|
|
setEditStrategy(QSqlTableModel::OnManualSubmit);
|
2018-07-22 16:47:55 +02:00
|
|
|
select();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SqlProfileModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (role < Qt::UserRole)
|
|
|
|
return QSqlTableModel::data(index, role);
|
|
|
|
|
|
|
|
const QSqlRecord sqlRecord = record(index.row());
|
|
|
|
return sqlRecord.value(role - Qt::UserRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> SqlProfileModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> names;
|
|
|
|
names[Qt::UserRole + 0] = "id";
|
|
|
|
names[Qt::UserRole + 1] = "name";
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
2018-08-03 15:18:23 +02:00
|
|
|
|
|
|
|
bool SqlProfileModel::append(QString name)
|
|
|
|
{
|
|
|
|
qDebug() << name;
|
|
|
|
QSqlRecord newRecord = record();
|
|
|
|
newRecord.setValue("name", name);
|
|
|
|
|
|
|
|
if (!insertRecord(rowCount(), newRecord)) {
|
|
|
|
qWarning() << "Failed to add profile:" << lastError().text();
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
submitAll();
|
|
|
|
return(true);
|
|
|
|
}
|
2018-08-11 23:54:34 +02:00
|
|
|
|
|
|
|
void SqlProfileModel::remove(int row)
|
|
|
|
{
|
|
|
|
removeRows(row, 1);
|
|
|
|
submitAll();
|
|
|
|
}
|