-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumbercounteritemwidget.cpp
More file actions
217 lines (175 loc) · 5.95 KB
/
Copy pathnumbercounteritemwidget.cpp
File metadata and controls
217 lines (175 loc) · 5.95 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "numbercounteritemwidget.h"
#include <QStyleOption>
#include <QPainter>
#include <QDir>
#include <QMessageBox>
#include "numbercounter.h"
NumberCounterItemWidget::NumberCounterItemWidget(const QString& strFileName,QWidget *parent)
: QWidget(parent),m_pMainWnd((NumberCounter*)parent), m_strFileName(strFileName)
{
if (!m_pMainWnd->m_stUserData.blDark)
{
m_colorForeground = Qt::black;
m_colorBackground = Qt::white;
}
QString strWidgetStyle = " QWidget { border: none; background: %1; }";
strWidgetStyle = strWidgetStyle.arg(m_colorBackground.name());
this->setStyleSheet(strWidgetStyle);
ui.setupUi(this);
this->Init();
}
NumberCounterItemWidget::~NumberCounterItemWidget()
{
}
void NumberCounterItemWidget::Init()
{
this->InitCtrl();
this->InitVar();
}
void NumberCounterItemWidget::InitCtrl()
{
m_pLineeditFileName = ui.lineEdit_filename;
m_pLineeditFileName->setToolTip("Double click to edit the file name.");
QFont ftLineEdit = qApp->font();
ftLineEdit.setPixelSize(16);
ftLineEdit.setBold(false);
m_pLineeditFileName->setFont(ftLineEdit);
this->FileNameLineEditStyle(m_pLineeditFileName);
m_pButIncrease = ui.pushButton_Increase;
this->ButCommonSet(m_pButIncrease);
this->ButStyle(m_pButIncrease);
m_pButDecrease = ui.pushButton_Decrease;
this->ButCommonSet(m_pButDecrease);
this->ButStyle(m_pButDecrease);
m_pLabelDigit = ui.label;
QFont ftLabel = qApp->font();
ftLabel.setBold(true);
ftLabel.setPixelSize(75);
m_pLabelDigit->setFont(ftLabel);
this->LabelStyle(m_pLabelDigit);
QObject::connect(m_pButIncrease, &QPushButton::clicked, [this]() {
this->ValChanged(1);
});
QObject::connect(m_pButDecrease, &QPushButton::clicked, [this]() {
this->ValChanged(-1);
});
QObject::connect(m_pLineeditFileName, &FileNameLineEdit::SignalNameChanged, [this](const QString& strNewName, const QString& strOldName) {
QString strErrMsg;
if (!m_pMainWnd->CheckNewFileName(strNewName,strOldName,strErrMsg))
{
QMessageBox errorBox;
errorBox.setIcon(QMessageBox::Critical);
errorBox.setWindowTitle("Error");
errorBox.setText(strErrMsg);
errorBox.setStandardButtons(QMessageBox::Ok);
errorBox.exec();
m_pLineeditFileName->setText(strOldName);
}
});
}
void NumberCounterItemWidget::InitVar()
{
if (m_strFileName.isEmpty())
{
QString strDefaultFileName = QString("Name_%1").arg(QString::number(10 + rand() % 10000));
m_pLineeditFileName->setText(strDefaultFileName);
m_pLabelDigit->setText(QString::number(0));
QString strLoc = m_pMainWnd->m_stUserData.strLocation;
if (strLoc.right(1) != QDir::separator())
{
strLoc += QDir::separator();
}
QString strFullFileName = QString("%1%2.txt").arg(strLoc).arg(strDefaultFileName);
m_strFileName = strFullFileName;
}
else
{
QFile file(m_strFileName);
if (file.open(QIODevice::ReadOnly))
{
QByteArray byteData = file.readAll();
m_pLabelDigit->setText(byteData);
file.close();
}
QFileInfo fileInfo(m_strFileName);
m_pLineeditFileName->setText(fileInfo.baseName());
}
m_pMainWnd->AddCounterFileName(m_strFileName);
}
void NumberCounterItemWidget::ButStyle(QPushButton* pBut)
{
QString strButStyle = "QPushButton {color:%1; border-radius: 12px; border: 4px solid %2;; background-color : %3 ;}";
strButStyle = strButStyle.arg(m_colorForeground.name()).arg(m_colorForeground.name()).arg(m_colorBackground.name());
pBut->setStyleSheet(strButStyle);
}
void NumberCounterItemWidget::LabelStyle(QLabel* pLabel)
{
QString strLabelStyle = "QLabel {color:%1; border-radius: 12px; border: 4px solid %2; background-color : %3 ;}";
strLabelStyle = strLabelStyle.arg(m_colorForeground.name()).arg(m_colorForeground.name()).arg(m_colorBackground.name());
pLabel->setStyleSheet(strLabelStyle);
}
void NumberCounterItemWidget::ButCommonSet(QPushButton* pBut)
{
pBut->setFixedSize(QSize(80, 36));
QFont ftBut = qApp->font();
ftBut.setBold(true);
ftBut.setPixelSize(50);
pBut->setFont(ftBut);
pBut->setFocusPolicy(Qt::NoFocus);
}
void NumberCounterItemWidget::ChangeTheme()
{
QColor colorTmp = m_colorForeground;
m_colorForeground = m_colorBackground;
m_colorBackground = colorTmp;
QString strWidgetStyle = " QWidget { border: none; background: %1; }";
strWidgetStyle = strWidgetStyle.arg(m_colorBackground.name());
this->setStyleSheet(strWidgetStyle);
this->ButStyle(m_pButDecrease);
this->ButStyle(m_pButIncrease);
this->LabelStyle(m_pLabelDigit);
this->FileNameLineEditStyle(m_pLineeditFileName);
}
void NumberCounterItemWidget::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void NumberCounterItemWidget::FileNameLineEditStyle(FileNameLineEdit* pLineEdit)
{
QString strLineEditStyle = "QLineEdit { padding-left: 8px ; color:%1; border: none; background-color : %2 ;}";
strLineEditStyle = strLineEditStyle.arg(m_colorForeground.name()).arg(m_colorBackground.name());
pLineEdit->setStyleSheet(strLineEditStyle);
}
void NumberCounterItemWidget::ValChanged(int nVal)
{
int nCurrVal = m_pLabelDigit->text().trimmed().toInt();
nCurrVal += nVal;
if (nCurrVal < 0)
{
nCurrVal = 0;
}
m_pLabelDigit->setText(QString::number(nCurrVal));
this->SaveVal2File();
}
void NumberCounterItemWidget::SaveVal2File()
{
QString strFileName = m_pLineeditFileName->text().trimmed();
int nVal = m_pLabelDigit->text().trimmed().toInt();
QString strLoc = m_pMainWnd->m_stUserData.strLocation;
if (strLoc.right(1) != QDir::separator())
{
strLoc += QDir::separator();
}
QString strFullFileName = QString("%1%2.txt").arg(strLoc).arg(strFileName);
QFile file(strFullFileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
file.write(QByteArray::number(nVal));
file.close();
}
m_pMainWnd->AddCounterFileName(strFullFileName);
}