summaryrefslogtreecommitdiff
path: root/engineeringpage.cpp
blob: 695c697439d5c66c31edf86544c1633b9f167ff8 (plain)
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
/*
    Class for making engineering pages
    Created by Joshua Drake
     6/9/2022
*/
#include "engineeringpage.h"

engineeringpage::engineeringpage(QWidget *parent) : QWidget(parent) ,
    rows(0)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    title->setText("Null");
    title->setFrameStyle(QFrame::NoFrame);
    title->setAlignment(Qt::AlignCenter  | Qt::AlignCenter );
    title->setGeometry(0,0,SCREEN_WIDTH,20);

    table->setAlternatingRowColors(true);
    table->setFrameStyle(QFrame::NoFrame);
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    initializestylesheet();
}
engineeringpage::engineeringpage(QString titletext, QStringList message_names,QWidget *parent) : QWidget(parent) ,
    rows(0)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    title->setText("Null");
    title->setFrameStyle(QFrame::NoFrame);
    title->setAlignment(Qt::AlignCenter  | Qt::AlignCenter );
    title->setGeometry(0,0,SCREEN_WIDTH,20);

    table->setAlternatingRowColors(true);
    table->setFrameStyle(QFrame::NoFrame);
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    initializestylesheet();
    configure(titletext, message_names);
}

void engineeringpage::initializestylesheet()
{
    table->setStyleSheet(
                        "QTableView"
                         "{"
                                "padding: 0px 0px 0px 0px;"
                                "background-color: black;"
                                "alternate-background-color: white;"
                                "selection-background-color: white;"
                                "/*border: 1px solid white;*/"
                                "border-width: 1px;"
                                "border-radius: 5px;"
                                "border-color: white;"
                                "border-style: outset;"
                         "}"
                         "QTableView::item"
                         "{"
                                "color: white;"
                                "background-color: black;"
                                "alternate-background-color: white;"
                                "border-bottom: 1px dotted white;"
                         "}"

                         "QHeaderView::section"
                         " {"
                                "background-color: black;"
                                "border-right: 1px solid white;"
                                "border-bottom: 1px solid white;"
                                "text-align: center;"
                                "padding: 0px 0px;"
                          "}");
}
void engineeringpage::configure(QString titletext, QStringList message_names)
{
   rows = message_names.length();
   title->setText(titletext);
   title->setGeometry(0,0,SCREEN_WIDTH,SCREEN_HEIGHT*.10);

    table->setColumnCount(1);
    table->setRowCount(rows);
    table->setFrameShape(QFrame::NoFrame);
    table->setGeometry(0,(SCREEN_HEIGHT*.10),SCREEN_WIDTH,SCREEN_HEIGHT - (SCREEN_HEIGHT*.10));
    table->setSelectionBehavior(QAbstractItemView::SelectRows);
    verticalheader = table->verticalHeader();
    verticalheader->setMinimumSectionSize(1);
    verticalheader->setDefaultSectionSize((SCREEN_HEIGHT - (SCREEN_HEIGHT*.10))/rows);
    verticalheader->setMaximumSectionSize(SCREEN_HEIGHT);

    //verticalheader->setSectionResizeMode(QHeaderView::Fixed);
    verticalheader->setAlternatingRowColors(true);
    verticalheader->setFixedWidth(SCREEN_WIDTH * .40);

    horizontalheader = table->horizontalHeader();
    horizontalheader->setSectionResizeMode(QHeaderView::Stretch);
    horizontalheader->setMaximumSectionSize(SCREEN_WIDTH);
    horizontalheader->setVisible(false);



     table->setVerticalHeaderLabels(message_names);
    // table->verticalHeader()->font().setCapitalization(true);
    QFont tableFont("System", verticalheader->height(), QFont::Bold, false);
    tableFont.setCapitalization(QFont::MixedCase);
    const int dataFontSize = table->verticalHeader()->rect().height();


    verticalheader->setFont(tableFont);
     for(int x = 0; x < rows; x++)
     {
         //qDebug() << message_names.at(x).length();
         /*if(message_names.at(x).length() > 10)tableFont.setPointSizeF((verticalheader->width())/(message_names.at(x).length()));
         else tableFont.setPixelSize((verticalheader->width())/(message_names.at(x).length())); //adjust font to be relative to header width and number of characters within the label
         */
         tableFont.setPointSizeF(table->verticalHeader()->rect().height());
         table->verticalHeaderItem(x)->setFont(tableFont);
         //table->verticalHeaderItem(x)->setTextAlignment(Qt::AlignCenter);


         QTableWidgetItem *instance = new QTableWidgetItem();
         QTableWidgetItem *v_instance = new QTableWidgetItem();
         tableFont.setPixelSize(dataFontSize);

         instance->setTextAlignment(Qt::AlignCenter);
         instance->setFont(tableFont);
         //instance->setText(message_names.at(x));
         v_instance->setTextAlignment(Qt::AlignCenter);
         v_instance->setFont(tableFont);
         v_instance->setText(message_names.at(x));
         tableHeader.push_back(v_instance);
         tableData.push_back(instance);

         if(textOverhang(tableHeader[x],message_names.at(x)))
         {
             v_instance->setTextAlignment(Qt::AlignLeft);
             v_instance->setTextAlignment(Qt::AlignVCenter);

             text_scrolling.insert(tableHeader[x],scrollText(message_names.at(x)));
         }
         table->setVerticalHeaderItem(x,tableHeader[x]);
         table->setItem(x,0,tableData[x]);

         //qDebug() << table->verticalHeaderItem(x) << " : " << table->item(x,0);
     }
     scroll_timer.setInterval(350);
     connect(&scroll_timer, SIGNAL(timeout()), this, SLOT(check_scroll()));
     scroll_timer.start();
}

void engineeringpage::update(int row, QString message_data)
{
    if(row >= rows) return;
    tableData[row]->setText(message_data);
}
void engineeringpage::update(QStringList message_data)
{
    for(int x = 0; (x < tableData.size()) && (x < message_data.size()); x++) tableData[x]->setText(message_data.at(x));
}
void engineeringpage::update(QList<double> message_data)
{
    for(int x = 0; (x < tableData.size()) && (x < message_data.size()); x++) tableData[x]->setData(Qt::DisplayRole,message_data.at(x));
    //qDebug() << message_data;
}
void engineeringpage::update(QByteArray message_data)
{
    for(int x = 0; x < (x < tableData.size()) && (x < message_data.size()); x++) tableData[x]->setData(Qt::DisplayRole,message_data.at(x));
    //qDebug() << message_data;
}
bool engineeringpage::textOverhang(QTableWidgetItem *instance, QString text)
{
    QFontMetrics fm(instance->font());

    if(fm.horizontalAdvance(text) > table->verticalHeader()->rect().width()) return true;
    return false;
}

void engineeringpage :: check_scroll(void)
{

    QMap<QTableWidgetItem*, scrollText>::iterator i;
    for (i = text_scrolling.begin(); i != text_scrolling.end(); ++i)
    {
        if(i.value().scrolled)
        {
            if(i.value().position > 0)
            {
                i.value().position--;
                i.key()->setText(i.value().fullText.right(i.value().fullText.length() - i.value().position));
            }
            else i.value().scrolled = false;
        }
        else
        {
            if(textOverhang(i.key(),i.value().fullText.right(i.value().fullText.length() - i.value().position)))
            {
                i.value().position++;
                i.key()->setText(i.value().fullText.right(i.value().fullText.length() - i.value().position));
            }
            else i.value().scrolled = true;
        }
    }
}