Skip to content

Commit

Permalink
Implement Add Profile button
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed May 31, 2024
1 parent aa7bdc1 commit 0c6eb58
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 32 deletions.
2 changes: 2 additions & 0 deletions application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ qt_add_qml_module(theterminal
QML_FILES settings/SingleProfileSettings.qml
SOURCES settings/fontmodel.h settings/fontmodel.cpp
SOURCES settings/colormodel.h settings/colormodel.cpp
SOURCES settings/profilesmodel.h settings/profilesmodel.cpp
SOURCES settings/profilehelper.h settings/profilehelper.cpp
)

set_target_properties(theterminal PROPERTIES
Expand Down
28 changes: 27 additions & 1 deletion application/settings/GeneralSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import QtQuick 2.15
import QtQuick.Layouts
import QtQuick.Controls
import com.vicr123.Contemporary
import com.vicr123.theterminal.libtheterminal
import Contemporary
import ".."

Item {
LayerCalculator {
Expand Down Expand Up @@ -57,7 +59,31 @@ Item {
}

ComboBox {
model: 5
id: defaultProfileSelection
model: ProfilesModel {

}
valueRole: "uuid"
displayText: selectedProfile.profileName

TerminalProfile {
id: selectedProfile
zoom: 1
profileUuid: defaultProfileSelection.currentValue
}

delegate: ItemDelegate {
required property var modelData
id: item
text: profile.profileName
width: parent.width

TerminalProfile {
id: profile
zoom: 1
profileUuid: item.modelData
}
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions application/settings/ProfileSettings.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick 2.15
import com.vicr123.Contemporary
import ".."

Item {
Component {
Expand All @@ -10,6 +11,10 @@ Item {
}
}

ProfileHelper {
id: profileHelper
}

Pager {
id: stack
anchors.fill: parent
Expand All @@ -21,6 +26,12 @@ Item {
"profileUuid": profile
})
}

onCreateNewProfile: () => {
stack.push(singleProfileComponent, {
"profileUuid": profileHelper.newProfileUuid()
})
}
}
}
}
36 changes: 29 additions & 7 deletions application/settings/RootProfileSettings.qml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import QtQuick 2.15
import QtQuick.Layouts
import QtQuick.Controls
import com.vicr123.theterminal.libtheterminal
import com.vicr123.Contemporary
import Contemporary
import ".."

Item {
id: root

signal openProfileSettings(string profile);
signal createNewProfile

LayerCalculator {
id: layer1
Expand Down Expand Up @@ -42,16 +45,12 @@ Item {

spacing: 10

model: ListModel {
ListElement {
name: "Default"
uuid: "EB31ADE1-9342-43E9-9E9C-811CCB978F64"
}
model: ProfilesModel {

}

delegate: Item {
id: item
required property var name;
required property var uuid;

implicitWidth: profileList.width
Expand All @@ -66,6 +65,12 @@ Item {

color: layer1.color

TerminalProfile {
id: profile
zoom: 1
profileUuid: item.uuid
}

ColumnLayout {
anchors.top: parent.top
anchors.left: parent.left
Expand All @@ -74,7 +79,7 @@ Item {
spacing: 10

Label {
text: item.name
text: profile.profileName
font.pointSize: 20
}

Expand All @@ -91,5 +96,22 @@ Item {
}
}
}

footer: Item {
implicitHeight: childrenRect.height + 6
implicitWidth: profileList.width
z: 20

Button {
id: addProfileButton
anchors.centerIn: parent
anchors.bottom: parent.top
implicitWidth: 600
text: qsTr("New Profile")
icon.name: "list-add"

onClicked: () => root.createNewProfile()
}
}
}
}
26 changes: 26 additions & 0 deletions application/settings/SingleProfileSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ Item {
Layout.preferredHeight: 10
}

GroupBox {
Layout.alignment: Qt.AlignHCenter
implicitWidth: 600
title: qsTr("Profile")

GridLayout {
anchors.left: parent.left
anchors.right: parent.right
columns: 2

Label {
text: qsTr("Name")
}

TextField {
id: profileNameField
text: profile.profileName
Layout.fillWidth: true
onEditingFinished: () => {
profile.profileName = profileNameField.text;
profile.saveProfile()
}
}
}
}

GroupBox {
Layout.alignment: Qt.AlignHCenter
implicitWidth: 600
Expand Down
11 changes: 11 additions & 0 deletions application/settings/profilehelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "profilehelper.h"

#include <QUuid>

ProfileHelper::ProfileHelper(QObject* parent) :
QObject{parent} {
}

QString ProfileHelper::newProfileUuid() {
return QUuid::createUuid().toString(QUuid::WithoutBraces);
}
18 changes: 18 additions & 0 deletions application/settings/profilehelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef PROFILEHELPER_H
#define PROFILEHELPER_H

#include <QObject>
#include <QQmlEngine>

class ProfileHelper : public QObject {
Q_OBJECT
QML_ELEMENT
public:
explicit ProfileHelper(QObject* parent = nullptr);

Q_SCRIPTABLE QString newProfileUuid();

signals:
};

#endif // PROFILEHELPER_H
77 changes: 77 additions & 0 deletions application/settings/profilesmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "profilesmodel.h"

#include <QDir>
#include <QFileSystemWatcher>
#include <QStandardPaths>

struct ProfilesModelPrivate {
QStringList profileUuids;
QFileSystemWatcher* watcher;
};

ProfilesModel::ProfilesModel(QObject* parent) :
QAbstractListModel(parent), d{new ProfilesModelPrivate()} {
auto profilesDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).absoluteFilePath("profiles");

if (!QDir(profilesDir).exists()) {
QDir::root().mkpath(profilesDir);
}

d->watcher = new QFileSystemWatcher(this);
d->watcher->addPath(profilesDir);
connect(d->watcher, &QFileSystemWatcher::directoryChanged, this, &ProfilesModel::updateProfiles);

updateProfiles();
}

ProfilesModel::~ProfilesModel() {
delete d;
}

int ProfilesModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid())
return 0;

return d->profileUuids.length();
}

QVariant ProfilesModel::data(const QModelIndex& index, int role) const {
if (!index.isValid())
return {};

auto uuid = d->profileUuids.at(index.row());
switch (role) {
case Roles::UuidRole:
return uuid;
}
return {};
}

void ProfilesModel::updateProfiles() {
QStringList foundProfiles;

QDir profilesDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).absoluteFilePath("profiles");
for (auto profile : profilesDir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot)) {
foundProfiles.append(profile.baseName());
if (!d->profileUuids.contains(profile.baseName())) {
beginInsertRows({}, d->profileUuids.length(), d->profileUuids.length());
d->profileUuids.append(profile.baseName());
endInsertRows();
}
}

for (auto i = 0; i < d->profileUuids.length(); i++) {
if (!foundProfiles.contains(d->profileUuids.at(i))) {
beginRemoveRows({}, i, i);
d->profileUuids.removeAt(i);
endRemoveRows();
i--;
}
}
}

QHash<int, QByteArray> ProfilesModel::roleNames() const {
return {
{Roles::UuidRole, "uuid"}
};
}
34 changes: 34 additions & 0 deletions application/settings/profilesmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef PROFILESMODEL_H
#define PROFILESMODEL_H

#include <QAbstractListModel>
#include <QQmlEngine>

struct ProfilesModelPrivate;
class ProfilesModel : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT

public:
explicit ProfilesModel(QObject* parent = nullptr);
~ProfilesModel();

enum Roles {
UuidRole = Qt::UserRole
};

// Basic functionality:
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;

private:
ProfilesModelPrivate* d;

void updateProfiles();

// QAbstractItemModel interface
public:
QHash<int, QByteArray> roleNames() const override;
};

#endif // PROFILESMODEL_H
Loading

0 comments on commit 0c6eb58

Please sign in to comment.