Files
Kareer/src/jobeditmodel.h
T
austinandClaude Opus 5 aa3a31f014 Fix the edit form so Save persists stage changes
Changing an application's stage and pressing Save closed the form
without saving the stage: JobsDatabase::updateJob() deliberately skips
the stage column so every move lands in stage_history, but
JobEditModel::save() never followed up with setStage(). It now does,
as the CLI's update command always has.

Also:
- Validate that company and title are non-empty and say so, instead of
  saving a blank field
- Always set lastError on failure, clear it on the next attempt, and
  bind the page's error message to it
- Reload the form's values when jobsModel is assigned, since QML does
  not guarantee it is set before editingJobId
- Bind the salary spin boxes to the model and write back only on user
  edits; guard the notes field the same way
- Add jobeditmodeltest covering load order, edits, stage history,
  salaries round-tripping, adding, and validation errors

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BxDf7HqD1xPnsZP8wt3NTk
2026-09-11 15:41:48 -05:00

86 lines
2.4 KiB
C++

/*
SPDX-FileCopyrightText: 2026 ToServeTheKing <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include "jobfieldcatalog.h"
#include "jobsmodel.h"
#include <QAbstractListModel>
#include <QHash>
#include <QQmlEngine>
#include <QVariant>
/**
* Drives the add/edit form the way FlatKontrol's PermissionsController drives
* PermissionsPage: a generic row model (one row per JobFieldCatalog::Field)
* that QML renders via Repeater + DelegateChooser on rowType, instead of
* each field being hand-written in QML.
*/
class JobEditModel : public QAbstractListModel
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(JobsModel *jobsModel READ jobsModel WRITE setJobsModel NOTIFY jobsModelChanged)
Q_PROPERTY(int editingJobId READ editingJobId WRITE setEditingJobId NOTIFY editingJobIdChanged)
Q_PROPERTY(QVariantList categories READ categories CONSTANT)
Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
public:
enum Roles {
FieldIdRole = Qt::UserRole + 1,
CategoryIdRole,
LabelRole,
RowTypeRole,
ValueRole,
ComboOptionsRole,
PlaceholderRole,
SpinMinRole,
SpinMaxRole,
};
Q_ENUM(Roles)
explicit JobEditModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
JobsModel *jobsModel() const;
void setJobsModel(JobsModel *model);
int editingJobId() const;
void setEditingJobId(int id);
QVariantList categories() const;
QString lastError() const;
/// Updates the value for the field at this row (called from the QML delegate).
Q_INVOKABLE void setValue(int row, const QVariant &value);
/// Persists the current values via jobsModel; true on success.
Q_INVOKABLE bool save();
Q_INVOKABLE bool deleteJob();
Q_SIGNALS:
void jobsModelChanged();
void editingJobIdChanged();
void lastErrorChanged();
private:
void resetValues();
void setLastError(const QString &error);
JobsModel *m_jobsModel = nullptr;
int m_editingJobId = -1;
QHash<QString, QVariant> m_values;
QString m_lastError;
QString m_loadedStage; ///< Stage as loaded from the database, to detect stage changes on save.
QList<JobFieldCatalog::Field> m_fields;
};