Skip to content

Commit 53ac305

Browse files
committed
GUI
• GUI progress bar • GUI console window • Console debug
1 parent 2353ba5 commit 53ac305

File tree

7 files changed

+133
-48
lines changed

7 files changed

+133
-48
lines changed

src/Solidify.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <iostream>
1919
#include <iomanip>
2020
#include <string>
21+
#include <math.h>
2122

2223
#include "Timer.h"
2324

@@ -29,17 +30,21 @@
2930

3031
using namespace OIIO;
3132

32-
//bool progress_callback(void* opaque_data, float portion_done)
33-
//{
34-
// const int width = 40;
35-
// int dashes = width * portion_done;
36-
// std::cout << "\r" << std::flush;
37-
// std::cout << '|' << std::left << std::setw(width) << std::string(dashes, '#') << '|' << std::setw(1);
38-
// return (portion_done >= 1.f);
39-
//}
40-
41-
bool progress_callback(void* opaque_data, float portion_done)
42-
{
33+
int hue = 186;
34+
35+
void pbar_color_rand(QProgressBar* progressBar) {
36+
//hue = QRandomGenerator::global()->bounded(360);
37+
hue = (hue + 45) % 360;
38+
int saturation = 250; // Set saturation value
39+
int value = 205; // Set value
40+
41+
QColor color;
42+
color.setHsv(hue, saturation, value);
43+
44+
setPBarColor(progressBar, color.name());
45+
}
46+
47+
bool progress_callback(void* opaque_data, float portion_done) {
4348
// Cast the opaque_data back to a QProgressBar
4449
QProgressBar* progressBar = static_cast<QProgressBar*>(opaque_data);
4550

@@ -54,12 +59,13 @@ bool progress_callback(void* opaque_data, float portion_done)
5459
return (portion_done >= 1.f);
5560
}
5661

57-
std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file) {
62+
std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file, MainWindow* mainWindow) {
5863
ImageBuf alpha_buf(mask_file);
5964
std::cout << "Reading " << mask_file << std::endl;
6065
bool read_ok = alpha_buf.read(0, 0, 0, 1, true, TypeDesc::FLOAT, nullptr, nullptr);
6166
if (!read_ok) {
6267
std::cerr << "Error: Could not read mask image\n";
68+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
6369
system("pause");
6470
exit(-1);
6571
}
@@ -80,6 +86,7 @@ std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file) {
8086
ok = ok && ImageBufAlgo::channel_append(rgb_alpha, temp_buff, alpha_buf);
8187
if (!ok) {
8288
std::cerr << "Error: Could not append channels\n";
89+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
8390
system("pause");
8491
exit(-1);
8592
}
@@ -89,7 +96,8 @@ std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file) {
8996
return { alpha_buf, rgb_alpha };
9097
}
9198

92-
bool solidify_main(const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair, QProgressBar* progressBar) {
99+
bool solidify_main(const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair,
100+
QProgressBar* progressBar, MainWindow* mainWindow) {
93101
Timer g_timer;
94102
// Create an ImageBuf object for the input file
95103
ImageBuf input_buf(inputFileName);
@@ -113,6 +121,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
113121
//bool read_ok = input_buf.read(0, 0, 0, last_channel, true, TypeUnknown, *progress_callback, nullptr);
114122
if (!read_ok) {
115123
std::cerr << "Error: Could not read input image\n";
124+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
116125
return false;
117126
}
118127

@@ -212,6 +221,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
212221
ok = ok && ImageBufAlgo::channel_append(rgba_buf, input_buf, *alpha_buf_ptr);
213222
if (!ok) {
214223
std::cerr << "Error: " << rgba_buf.geterror() << std::endl;
224+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
215225
return false;
216226
}
217227
// rename last channel to alpha and set alpha channel
@@ -226,6 +236,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
226236

227237
if (!ok) {
228238
std::cerr << "Error: " << result_buf.geterror() << std::endl;
239+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
229240
return false;
230241
}
231242

@@ -234,6 +245,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
234245
auto out = ImageOutput::create(outputFileName);
235246
if (!out) {
236247
std::cerr << "Could not create output file: " << outputFileName << std::endl;
248+
mainWindow->emitUpdateTextSignal("Error! Check console for details");
237249
return false;
238250
}
239251

@@ -256,7 +268,12 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
256268
out->close();
257269

258270
std::cout << std::endl << "Total processing time : " << g_timer.nowText() << std::endl;
271+
272+
pbar_color_rand(progressBar);
259273
QMetaObject::invokeMethod(progressBar, "setValue", Qt::QueuedConnection, Q_ARG(int, 0));
260274

275+
//QString DebugText = QString("Total processing time : %1").arg(QString::fromStdString(g_timer.nowText()));
276+
//mainWindow->emitUpdateTextSignal(DebugText);
277+
261278
return true;
262279
}

src/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
#include "stdafx.h"
2020

2121
int main(int argc, char* argv[]) {
22-
// create console
22+
// Allocate console and redirect std output
2323
AllocConsole();
2424
freopen("CONOUT$", "w", stdout);
2525
freopen("CONOUT$", "w", stderr);
26+
27+
ShowWindow(GetConsoleWindow(), SW_HIDE);
28+
2629
QApplication app(argc, argv);
2730

2831
MainWindow window;

src/processing.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ QString checkAlpha(std::vector<QString> fileNames) {
4444
return "";
4545
}
4646

47-
bool doProcessing(QList<QUrl> urls, QProgressBar* progressBar) {
48-
49-
47+
bool doProcessing(QList<QUrl> urls, QProgressBar* progressBar, MainWindow* mainWindow) {
5048
std::vector<QString> fileNames; // This will hold the names of all files
5149

5250
for (const QUrl& url : urls) {
@@ -63,7 +61,7 @@ bool doProcessing(QList<QUrl> urls, QProgressBar* progressBar) {
6361

6462
if (mask_file != "") {
6563
qDebug() << "Mask file: " << mask_file << " will be used.";
66-
mask_pair = mask_load(mask_file.toStdString());
64+
mask_pair = mask_load(mask_file.toStdString(), mainWindow);
6765
}
6866

6967
for (int i = 0; i < fileNames.size(); i++) {
@@ -76,20 +74,24 @@ bool doProcessing(QList<QUrl> urls, QProgressBar* progressBar) {
7674
QString baseName = fileInfo.baseName();
7775
QString path = fileInfo.absolutePath();
7876
QString outName = path + "/" + baseName + "_fill." + fileInfo.completeSuffix();
79-
80-
//std::string infile = static_cast<std::string>(fileNames[i].toUtf8().constData());
81-
//std::string outfile = static_cast<std::string>(outName.toUtf8().constData());
77+
8278
std::string infile = fileNames[i].toStdString();
8379
std::string outfile = outName.toStdString();
8480

81+
QString DebugText = "Source: " + fileInfo.fileName() +
82+
"\nTarget: " + baseName + "_fill." + fileInfo.completeSuffix() +
83+
"\nMask: " + QFileInfo(mask_file).baseName();
84+
85+
mainWindow->emitUpdateTextSignal(DebugText);
86+
8587
// Call the solidify_main function
86-
bool result = solidify_main(infile, outfile, mask_pair, progressBar);
88+
bool result = solidify_main(infile, outfile, mask_pair, progressBar, mainWindow);
8789

8890
if (!result) {
8991
system("pause");
9092
exit(-1);
9193
};
9294
}
93-
95+
mainWindow->emitUpdateTextSignal("Everything Done!");
9496
return true;
9597
}

src/processing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#pragma once
1919
#include "ui.h"
2020

21-
bool doProcessing(QList<QUrl> URLs, QProgressBar* progressBar);
21+
class MainWindow; // forward declaration
22+
23+
bool doProcessing(QList<QUrl> URLs, QProgressBar* progressBar, MainWindow* mainWindow);
2224

2325
QString checkAlpha(std::vector<QString> fileNames);

src/solidify.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
using namespace OIIO;
2626

27-
std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file);
27+
bool progress_callback(void* opaque_data, float portion_done);
2828

29-
bool solidify_main(const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair, QProgressBar* progressBar);
29+
std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file, MainWindow* mainWindow);
30+
31+
bool solidify_main(const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair,
32+
QProgressBar* progressBar, MainWindow* mainWindow);

src/ui.cpp

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "processing.h"
55

6+
class MainWindow;
7+
68
DropArea::DropArea() {
79
QVBoxLayout* layout = new QVBoxLayout;
810
QLabel* label = new QLabel("Drag & drop files here");
@@ -41,51 +43,76 @@ MainWindow::MainWindow() {
4143

4244
layout->addWidget(dropArea); // Add the drop area to the layout
4345

44-
// Create a new QPlainTextEdit
45-
QPlainTextEdit* textOutput = new QPlainTextEdit;
46-
textOutput->setReadOnly(true); // Make it read-only so users can't edit the text
47-
// set the size to fill width and 1/3 of the height
48-
textOutput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
49-
textOutput->setFixedHeight(50);
50-
textOutput->setStyleSheet("border-radius: 3px; background-color: #E0E0E0; margin-bottom: 4px;");
51-
// add some text to the text output
52-
textOutput->setPlainText("Debug:\n");
53-
54-
layout->addWidget(textOutput); // Add the textOutput to the layout
55-
46+
// Progress bar
5647
progressBar = new QProgressBar;
5748
progressBar->setFixedHeight(20);
5849
progressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
5950
progressBar->setRange(0, 100);
6051
//progressBar->setValue(75);
6152
progressBar->setTextVisible(false);
6253
progressBar->setStyleSheet(
63-
"QProgressBar {border: 0px solid black; border-radius: 3px; background-color: white; color: black;}"
64-
"QProgressBar::chunk {background-color: #05B8CC;}");
65-
layout->addWidget(progressBar); // Add the progress bar to the layout
54+
"QProgressBar {border: 0px solid black; border-radius: 3px; background-color: white; color: black; margin-bottom: 4px;}"
55+
"QProgressBar::chunk {background-color: #05B8CC;}");
56+
layout->addWidget(progressBar);
57+
58+
// Create a new QPlainTextEdit
59+
QPlainTextEdit* textOutput = new QPlainTextEdit;
60+
textOutput->setReadOnly(true); // Make it read-only so users can't edit the text
61+
// set the size to fill width and 1/3 of the height
62+
textOutput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
63+
textOutput->setFixedHeight(52);
64+
textOutput->setStyleSheet("border-radius: 3px; background-color: #E0E0E0; padding-left: 4px; padding-right: 4px;");
65+
// text size
66+
QFont font = this->font();
67+
font.setPointSize(8);
68+
font.setStyleHint(QFont::Monospace);
69+
textOutput->setFont(font);
70+
71+
textOutput->setPlainText("Waiting for user inputs...");
72+
textOutput->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
73+
layout->addWidget(textOutput);
74+
75+
// widgets
6676

6777
QWidget* centralWidget = new QWidget;
6878
centralWidget->setLayout(layout); // Set the layout for the central widget
6979
setCentralWidget(centralWidget); // Set the central widget of the MainWindow
7080

7181
QMenuBar* menuBar = new QMenuBar;
7282
QMenu* f_menu = new QMenu("Files", menuBar);
73-
QMenu* r_menu = new QMenu("Reset", menuBar);
74-
QAction* f_Restart = new QAction("Restart", r_menu);
83+
QMenu* s_menu = new QMenu("Settings", menuBar);
84+
//QMenu* r_menu = new QMenu("Reset", menuBar);
85+
//QAction* f_Restart = new QAction("Restart", r_menu);
86+
QAction* s_Settings = new QAction("Enable Console", s_menu);
87+
s_Settings->setCheckable(true);
88+
s_Settings->setChecked(false);
89+
QAction* f_Restart = new QAction("Restart", f_menu);
7590
QAction* f_Exit = new QAction("Exit", f_menu);
76-
r_menu->addAction(f_Restart);
91+
//r_menu->addAction(f_Restart);
92+
s_menu->addAction(s_Settings);
93+
f_menu->addAction(f_Restart);
94+
f_menu->addSeparator();
7795
f_menu->addAction(f_Exit);
7896
menuBar->addMenu(f_menu);
79-
menuBar->addMenu(r_menu);
97+
menuBar->addMenu(s_menu);
8098
setMenuBar(menuBar);
8199

82100
setWindowFlags(Qt::WindowStaysOnTopHint);
83-
setWindowTitle("Solidify 1.22");
84-
setFixedSize(400, 400);
101+
setWindowTitle("Solidify 1.30");
102+
setFixedSize(440, 440);
85103

104+
// Add a flag to indicate whether the console has been allocated
105+
bool consoleAllocated = false;
106+
86107
// Connect the signal from the drop area to the slot in the main window
87108
connect(dropArea, &DropArea::filesDropped, this, &MainWindow::startProcessing);
88109

110+
// Connect the Settings action's triggered signal to a slot that toggles the console
111+
connect(s_Settings, &QAction::toggled, this, &MainWindow::toggleConsole);
112+
113+
// Add new connection for updating the textOutput
114+
connect(this, &MainWindow::updateTextSignal, textOutput, &QPlainTextEdit::setPlainText);
115+
89116
// Connect the Exit action's triggered signal to QApplication's quit slot
90117
connect(f_Exit, &QAction::triggered, qApp, &QApplication::quit);
91118

@@ -95,16 +122,39 @@ MainWindow::MainWindow() {
95122

96123
void MainWindow::startProcessing(QList<QUrl> urls) {
97124
// Move the processing to a separate thread
98-
QFuture<bool> future = QtConcurrent::run(doProcessing, urls, progressBar);
125+
QFuture<bool> future = QtConcurrent::run(doProcessing, urls, progressBar, this);
99126

100127
processingWatcher.setFuture(future);
101128

102129
connect(&processingWatcher, &QFutureWatcher<bool>::progressValueChanged, progressBar, &QProgressBar::setValue);
103130
}
104131

132+
void MainWindow::toggleConsole(bool checked) {
133+
if (checked) {
134+
// Show console
135+
ShowWindow(GetConsoleWindow(), SW_SHOW);
136+
}
137+
else {
138+
// Hide console
139+
ShowWindow(GetConsoleWindow(), SW_HIDE);
140+
}
141+
}
142+
105143
void MainWindow::restartApp() {
106144
QStringList arguments;
107145
QProcess::startDetached(QApplication::applicationFilePath(), arguments);
108146

109147
QApplication::quit();
148+
}
149+
150+
void setPBarColor(QProgressBar* progressBar, const QColor& color) {
151+
QString style = QString(
152+
"QProgressBar {"
153+
"border: 0px solid black; border-radius: 3px; background-color: white; color: black;"
154+
"margin-bottom: 4px;"
155+
"}"
156+
"QProgressBar::chunk {background-color: %1;}"
157+
).arg(color.name());
158+
159+
progressBar->setStyleSheet(style);
110160
}

src/ui.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <QtCore/QFutureWatcher>
2121
#include <QtConcurrent/QtConcurrentRun>
22+
#include <QtCore/QRandomGenerator>
2223

2324
#include "processing.h"
2425

@@ -40,12 +41,19 @@ class MainWindow : public QMainWindow {
4041
Q_OBJECT // Macro needed to handle signals and slots
4142
public:
4243
MainWindow();
44+
void emitUpdateTextSignal(const QString& text) { emit updateTextSignal(text); } // Public method to emit the signal
45+
46+
signals:
47+
void updateTextSignal(const QString& text);
4348

4449
private slots:
4550
void restartApp();
51+
void toggleConsole(bool cheked);
4652
void startProcessing(QList<QUrl> urls); // New slot
4753

4854
private:
4955
QFutureWatcher<bool> processingWatcher;
5056
QProgressBar* progressBar;
5157
};
58+
59+
void setPBarColor(QProgressBar* progressBar, const QColor& color = QColor("#05B8CC"));

0 commit comments

Comments
 (0)