Skip to content

Commit

Permalink
Add tab select & errors
Browse files Browse the repository at this point in the history
Signed-off-by: swurl <[email protected]>
  • Loading branch information
crueter committed Nov 21, 2024
1 parent 3b6d7c4 commit df3b9b8
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 9 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ add_subdirectory("program_info")
find_package(ntcore)
find_package(Protobuf)

set(QRC_FILES fields.qrc
accents.qrc)
set(QRC_FILES
fields.qrc
accents.qrc
icons.qrc)

qt_add_executable(${Dashboard_EXEC_NAME}
main.cpp
Expand Down Expand Up @@ -96,6 +98,7 @@ set(QML_ELEMENTS
widgets/sendable/StringChooser

widgets/misc/CameraView
widgets/misc/ErrorsWidget

dialogs/TopicView
dialogs/TabNameDialog
Expand Down
2 changes: 1 addition & 1 deletion dialogs/TopicView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import QFRCDashboard

Row {
id: tv
z: 4
z: 5

property alias menuAnim: menuAnim

Expand Down
7 changes: 7 additions & 0 deletions icons.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<RCC>
<qresource prefix="/">
<file alias="Warning">icons/warn.svg</file>
<file alias="Information">icons/info.svg</file>
<file alias="Critical">icons/crit.svg</file>
</qresource>
</RCC>
25 changes: 25 additions & 0 deletions icons/crit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions icons/info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions icons/warn.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions include/models/TabListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TabListModel : public QAbstractListModel
Q_OBJECT
QML_ELEMENT

Q_PROPERTY(int selectedTab READ selectedTab NOTIFY selectedTabChanged FINAL)
public:
enum TLMRoleTypes {
TITLE = Qt::UserRole,
Expand Down Expand Up @@ -56,12 +57,19 @@ class TabListModel : public QAbstractListModel
Q_INVOKABLE void loadObject(const QJsonDocument &doc);
Q_INVOKABLE void load(const QString &fileName = "");

int selectedTab() const;
void selectTab(const QString &tab);

signals:
void selectedTabChanged();

protected:
QHash<int, QByteArray> roleNames() const override;

private:
QList<Tab> m_data;

SettingsManager *m_settings;
int m_selectedTab = 0;
};
#endif // TABLISTMODEL_H
8 changes: 7 additions & 1 deletion items/MainScreen.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ Rectangle {
widgetConf.openUp(item)
}

function setTab() {
swipe.setCurrentIndex(tlm.selectedTab)
}

Component.onCompleted: {
if (settings.loadRecent && !settings.recentFiles.empty) {
filename = settings.recentFiles[0]
if (filename === "" || filename === null) return;
tlm.load(filename)
}

tlm.onSelectedTabChanged.connect(setTab)
}

function drag(pos, fromList) {
Expand Down Expand Up @@ -76,7 +82,7 @@ Rectangle {
w.mrow = p.x
w.mcolumn = p.y

w.z = 2
w.z = 3
w.visible = true
w.cancelDrag()
}
Expand Down
15 changes: 15 additions & 0 deletions items/Tab.qml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ Rectangle {
Layout.preferredHeight: grid.prefHeight(this)
}
}

DelegateChoice {
roleValue: "errors"
ErrorsWidget {
Layout.row: model.row
Layout.column: model.column
Layout.rowSpan: model.rowSpan
Layout.columnSpan: model.colSpan

Layout.margins: 8

Layout.preferredWidth: grid.prefWidth(this)
Layout.preferredHeight: grid.prefHeight(this)
}
}
}
}

Expand Down
95 changes: 95 additions & 0 deletions java/QFRCLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot;

import java.util.LinkedList;
import java.util.Queue;

import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;

/** Class for interacting with QFRCDashboard. */
public class QFRCLib {
public enum ErrorLevel {
Information,
Warning,
Critical
}

private static class Error {
private final String m_message;
private final ErrorLevel m_level;

public Error(String message, ErrorLevel level) {
m_message = message;
m_level = level;
}

public String getMessage() {
return m_message;
}

public ErrorLevel getLevel() {
return m_level;
}
}

private static final NetworkTableInstance inst = NetworkTableInstance.getDefault();
private static final NetworkTable table = inst.getTable("QFRCDashboard");

private static final NetworkTableEntry tabEntry = table.getEntry("Tab");
private static final NetworkTableEntry errorsEntry = table.getEntry("Errors");

private static final Queue<Error> errors = new LinkedList<>();
private static int errorHistoryLength = 5;

/**
* Reports an error or info to be displayed on the dashboard.
* @param level The error level (info, warning, critical)
* @param message The error message.
*/
public static void reportError(ErrorLevel level, String message) {
Error e = new Error(message, level);
errors.offer(e);
if (errors.size() > errorHistoryLength) {
errors.poll();
}

publishErrors();
}

private static void publishErrors() {
String[] array = new String[errors.size() * 2];
int i = 0;
for (Error e : errors) {
array[i] = e.getLevel().toString();
++i;
array[i] = e.getMessage().toString();
++i;
}
errorsEntry.setStringArray(array);
}

/**
* Sets the length of the error queue.
* @param length The length of the error queue.
*/
public static void setErrorHistoryLength(int length) {
errorHistoryLength = length;
while (errors.size() > errorHistoryLength) {
errors.poll();
}
}

/**
* Sets the dashboard to the specified tab.
* @param tabName The tab to switch to.
* @apiNote This is a no-op if the specified tab does not exist.
*/
public static void setTab(String tabName) {
tabEntry.setString(tabName);
}
}
10 changes: 10 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ int main(int argc, char *argv[])
}
});

nt::NetworkTableEntry tabEntry = Globals::inst.GetEntry("/QFRCDashboard/Tab");
Globals::inst.AddListener(tabEntry, nt::EventFlags::kValueAll, [tlm] (const nt::Event &event) {
std::string_view value = event.GetValueEventData()->value.GetString();
QString qvalue = QString::fromStdString(std::string{value});

QMetaObject::invokeMethod(tlm, [tlm, qvalue] {
tlm->selectTab(qvalue);
});
});

qmlRegisterUncreatableMetaObject(
QFDFlags::staticMetaObject,
"QFDFlags",
Expand Down
19 changes: 19 additions & 0 deletions src/models/TabListModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ void TabListModel::load(const QString &filename)
file.close();
}

int TabListModel::selectedTab() const
{
return m_selectedTab;
}

void TabListModel::selectTab(const QString &tab)
{
for (size_t i = 0; i < rowCount(); ++i) {
Tab t = m_data.at(i);
if (t.title == tab) {
m_selectedTab = i;
emit selectedTabChanged();
return;
}
}

qWarning() << "Selected tab" << tab << "does not exist.";
}

QHash<int, QByteArray> TabListModel::roleNames() const
{
QHash<int,QByteArray> rez;
Expand Down
1 change: 1 addition & 0 deletions src/stores/TopicStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ QString TopicStore::typeString(QString topic)
case nt::NetworkTableType::kFloat: return "double";
case nt::NetworkTableType::kString: return "string";
case nt::NetworkTableType::kInteger: return "int";
case nt::NetworkTableType::kStringArray: return "errors";
default:
return "";
}
Expand Down
10 changes: 5 additions & 5 deletions widgets/BaseWidget.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Rectangle {
id: rect
width: 100
height: 100
z: 2
z: 3

radius: 12

Expand Down Expand Up @@ -140,7 +140,7 @@ Rectangle {

rect.beginDrag = Qt.point(parent.x, parent.y)
rect.Drag.hotSpot = Qt.point(mouse.x, mouse.y)
parent.z = 3
parent.z = 4

resizeBegin(rect.Drag)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ Rectangle {
rect.Drag.drop()

// dragEnd(rect.Drag)
parent.z = 2
parent.z = 3
cancelDrag()
}
}
Expand Down Expand Up @@ -236,7 +236,7 @@ Rectangle {
rect.beginDrag = Qt.point(rect.x, rect.y)
rect.beginResize = Qt.rect(rect.x, rect.y, rect.width, rect.height)
rect.Drag.hotSpot = Qt.point(mouse.x, mouse.y)
rect.z = 3
rect.z = 4

resizeBegin(rect.Drag)
}
Expand All @@ -259,7 +259,7 @@ Rectangle {

resizeEnd(rect.Drag)

rect.z = 2
rect.z = 3
}
}

Expand Down
Loading

0 comments on commit df3b9b8

Please sign in to comment.