From 90286b68532187e9367fe31c13c6cb37f578309e Mon Sep 17 00:00:00 2001 From: Hane Date: Wed, 3 Apr 2024 23:53:33 +0200 Subject: [PATCH] dynamic height + width 1st commit --- src/qt/qtclasses.cpp | 149 ++++++++++++++++++++++++------------------- src/qt/qtclasses.h | 62 +++--------------- src/qtestmain.cpp | 4 +- 3 files changed, 97 insertions(+), 118 deletions(-) diff --git a/src/qt/qtclasses.cpp b/src/qt/qtclasses.cpp index 3887cb0..6e5e4ff 100644 --- a/src/qt/qtclasses.cpp +++ b/src/qt/qtclasses.cpp @@ -25,7 +25,7 @@ void ExtendedCheckBox::customEvent(QEvent* ev) { QCheckBox::customEvent(ev); } -QRect MainWindow::setSizePosition() { +QRect MainWindow::setSizePosition(int width, int height) { //setGeometry ignores decoration size xdddd QRect trayIconPos = this->trayIcon->geometry(); int tix1, tix2, tiy1, tiy2; @@ -45,29 +45,51 @@ QRect MainWindow::setSizePosition() { switch (pos) { case SpawnPos::UP | SpawnPos::RIGHT: - return QRect((arx2 - windowWidth), ary1, windowWidth, 440); + return QRect((arx2 - width), ary1, width, height); break; case SpawnPos::DOWN | SpawnPos::LEFT: - return QRect(arx1, (ary2-440), windowWidth, 440); + return QRect(arx1, (ary2-height), width, height); break; case SpawnPos::DOWN | SpawnPos::RIGHT: - return QRect((arx2 - windowWidth), (ary2-440), windowWidth, 440); + return QRect((arx2 - width), (ary2-height), windowWidth, height); break; default: - return QRect(500, 400, windowWidth, 440); + return QRect(500, 400, width, height); break; } } +void MainWindow::calculateChildWidgetsSize() { + //We need dynamically added child widgets to expand so that we know their height + //TODO: MenuBar height + this->setAttribute(Qt::WA_DontShowOnScreen, true); + this->show(); + this->layout()->invalidate(); + this->hide(); + this->setAttribute(Qt::WA_DontShowOnScreen, false); + + int height = 0, width = 0; + for (auto *epw : this->ews) { + height += epw->height(); + width = (epw->width() > width) ? epw->width() : width; + } + + /* + * Establishing initial window size and position + */ + //TODO: test. hardcode. var. + setGeometry(setSizePosition(width, height)); +} + SessionWidget::SessionWidget(uint64_t idx, SessionHandler* sh, QWidget *parent) : QWidget(parent){ //todo: based on qgridlayout, name+mute should be its own widget, same with channels this->idx = idx; this->sh = sh; - layout = new QHBoxLayout(this); - //layout->setSizeConstraint(QLayout::SetFixedSize); - layout->setSizeConstraint(QLayout::SetMinAndMaxSize); - //layout->setMaximumSize(minimumSize()); + widgetLayout = new QHBoxLayout(this); + //widgetLayout->setSizeConstraint(QLayout::SetFixedSize); + widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); + //widgetLayout->setMaximumSize(minimumSize()); //this->setLayout( muteButton = new QCheckBox(this); @@ -103,12 +125,12 @@ SessionWidget::SessionWidget(uint64_t idx, SessionHandler* sh, QWidget *parent) * * layout->setSizeConstraint(QLayout::SetMinAndMaxSize); */ - layout->addItem(new QSpacerItem(200, 1, QSizePolicy::Expanding, QSizePolicy::Minimum)); - layout->addWidget(mainLabel, Qt::AlignLeft | Qt::AlignBottom); - layout->addWidget(muteButton, Qt::AlignRight | Qt::AlignBottom); - layout->addWidget(mainSlider, Qt::AlignRight | Qt::AlignBottom); + widgetLayout->addItem(new QSpacerItem(200, 1, QSizePolicy::Expanding, QSizePolicy::Minimum)); + widgetLayout->addWidget(mainLabel, Qt::AlignLeft | Qt::AlignBottom); + widgetLayout->addWidget(muteButton, Qt::AlignRight | Qt::AlignBottom); + widgetLayout->addWidget(mainSlider, Qt::AlignRight | Qt::AlignBottom); - //layout->setSizeConstraint(QLayout::SetMinAndMaxSize); + //widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); //TODO:0 = mute and muted, change volume = unmuted are client side tricks = 2 callbacks, one for volume, one for mute state. Implement as an user selectable option? connect(mainSlider, &QSlider::valueChanged, this,&SessionWidget::updateMainVolume); @@ -157,7 +179,7 @@ SessionWidget::~SessionWidget() { ChannelWidget::ChannelWidget(uint32_t channelCount, EndpointHandler* eph, QWidget *parent) : QWidget(parent){ this->eph = eph; this->channelCount = channelCount; - layout = new QGridLayout(this); + widgetLayout = new QGridLayout(this); float volume = 100; /* * Channel sliders setup @@ -176,8 +198,8 @@ ChannelWidget::ChannelWidget(uint32_t channelCount, EndpointHandler* eph, QWidge tmpLb->setText(QString::number(volume)); this->channelSliders.push_back(tmp); this->channelLabels.push_back(tmpLb); - layout->addWidget(tmp, 0, i); - layout->addWidget(tmpLb, 1, i); + widgetLayout->addWidget(tmp, 0, i); + widgetLayout->addWidget(tmpLb, 1, i); //TODO: check if there's a need to prevent deadlocks; probably this will eventually turn into its own func //this causes channel bar desync when back -> front. blocksignals below fix it. huh. @@ -186,7 +208,7 @@ ChannelWidget::ChannelWidget(uint32_t channelCount, EndpointHandler* eph, QWidge this->channelLabels.at(i)->setText(QString::number(newValue)); }); } - this->setLayout(layout); + this->setLayout(widgetLayout); } @@ -206,9 +228,9 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare this->eph->setState(EndpointState::ENDPOINT_ACTIVE, idx); //setAttribute(Qt::WA_TranslucentBackground); - layout = new QGridLayout(this); - //this->setLayout(layout); - log_debugcpp("epw main layout parent: " + std::to_string((intptr_t)(layout->parent()))); + widgetLayout = new QGridLayout(this); + //this->setLayout(widgetLayout); + log_debugcpp("epw main layout parent: " + std::to_string((intptr_t)(widgetLayout->parent()))); if (parent == nullptr) { log_debugcpp("ayooooo?"); } defaultRolesCheckBoxes = { @@ -226,8 +248,8 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare mainSlider = new QSlider(Qt::Horizontal, this); if (this->eph->getState() != EndpointState::ENDPOINT_ACTIVE) { - layout->addWidget(mainLabel, row, 0); - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 0); + widgetLayout->addWidget(mainLabel, row, 0); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 0); return; } @@ -251,10 +273,10 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare //tip: would need to be new widget with layout in it //mainMuteLayout = new QGridLayout(); - layout->addWidget(mainLabel, row, 0, Qt::AlignLeft | Qt::AlignVCenter); - layout->addWidget(muteButton, row, 1, Qt::AlignLeft | Qt::AlignVCenter); - layout->addWidget(mainSlider, row, 2, 1, 2, Qt::AlignLeft | Qt::AlignVCenter); - layout->setSizeConstraint(QLayout::SetMinAndMaxSize); + widgetLayout->addWidget(mainLabel, row, 0, Qt::AlignLeft | Qt::AlignVCenter); + widgetLayout->addWidget(muteButton, row, 1, Qt::AlignLeft | Qt::AlignVCenter); + widgetLayout->addWidget(mainSlider, row, 2, 1, 2, Qt::AlignLeft | Qt::AlignVCenter); + widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); //int debug2 = this->minimumWidth(); row++; @@ -270,7 +292,7 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare uint32_t epChannelCount = eph->getChannelCount(); if(epChannelCount) { cw = new ChannelWidget(epChannelCount, eph, this); - layout->addWidget(cw, row++, 0, 1, 4 /*colmax*/); + widgetLayout->addWidget(cw, row++, 0, 1, 4 /*colmax*/); } /* @@ -309,10 +331,10 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare this->eph->setRoles(Roles::ROLE_COMMUNICATIONS); }); - layout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_ALL), row, 0); - layout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE), row, 1); - layout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA), row, 2); - layout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS), row, 3); + widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_ALL), row, 0); + widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE), row, 1); + widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA), row, 2); + widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS), row, 3); row++; /* ----------------------------------------------------------- */ @@ -347,7 +369,7 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare /* First SessionWidget batch */ for (size_t i = 0; i < eph->getSessionCount(); i++) { SessionWidget* sessionWidget = new SessionWidget(i, eph->getSessionHandlers().at(i), this); - layout->addWidget(sessionWidget, row, 0, 1, 4); + widgetLayout->addWidget(sessionWidget, row, 0, 1, 4); row++; sessionWidgets.push_back(sessionWidget); eph->getSessionHandlers().at(i)->setFrontIndex(i); @@ -363,10 +385,10 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare }); //todo parent? - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 0); - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 4, 0); - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 6, 0); - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 6, 1); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 0); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 4, 0); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 6, 0); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 6, 1); log_debugcpp("ENDPOINT_WIDGETED"); } @@ -374,7 +396,7 @@ void EndpointWidget::addSessionWidget(CustomWidgetEvent* ev){ uint64_t index = this->sessionWidgets.size(); SessionWidget* sw = new SessionWidget(index, ev->payload, this); ev->payload->setFrontIndex(index); - this->layout->addWidget(sw, row, 0, 1, 4); + this->widgetLayout->addWidget(sw, row, 0, 1, 4); row++; sessionWidgets.push_back(sw); return; @@ -383,7 +405,7 @@ void EndpointWidget::addSessionWidget(CustomWidgetEvent* ev){ void EndpointWidget::removeSessionWidget(CustomWidgetEvent* ev){ uint64_t i = ev->payload->getFrontIndex(); this->sessionWidgets.at(i)->setParent(nullptr); - this->layout->removeWidget(sessionWidgets.at(i)); + this->widgetLayout->removeWidget(sessionWidgets.at(i)); delete sessionWidgets.at(i); sessionWidgets.at(i) = nullptr; ev->payload->setFrontIndex(INT_MAX); @@ -423,7 +445,7 @@ void MainWindow::customEvent(QEvent* ev) { void MainWindow::removeEndpointWidget(CustomWidgetEvent* ev){ uint64_t i = ev->payload; this->ews.at(i)->setParent(nullptr); - this->layout->removeWidget(ews.at(i)); + this->widgetLayout->removeWidget(ews.at(i)); //uint64_t saisu = ews.size(); //delete ews.at(index); delete ews.at(i); @@ -434,7 +456,7 @@ void MainWindow::removeEndpointWidget(CustomWidgetEvent* ev){ void MainWindow::addEndpointWidget(CustomWidgetEvent* ev){ EndpointWidget* epw = new EndpointWidget(this->ews.size(), ev->payload, widget); - this->layout->addWidget(epw); + this->widgetLayout->addWidget(epw); ews.push_back(epw); return; } @@ -533,7 +555,7 @@ std::map EndpointWidget::getDefaultRolesWidgets() { } HeaderWidget::HeaderWidget(QWidget *parent) : QWidget(parent) { - layout = new QGridLayout(this); + widgetLayout = new QGridLayout(this); QString text = "&" STRING_ABOUT; about = new QPushButton(text, this); @@ -544,18 +566,18 @@ HeaderWidget::HeaderWidget(QWidget *parent) : QWidget(parent) { text = "&" STRING_STARTUP; startup = new QPushButton(text, this); - layout->addWidget(openCP , 0, 0); - layout->addWidget(startup, 0, 1); + widgetLayout->addWidget(openCP , 0, 0); + widgetLayout->addWidget(startup, 0, 1); #endif - layout->addWidget(about , 0, 2); - this->setLayout(layout); + widgetLayout->addWidget(about , 0, 2); + this->setLayout(widgetLayout); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { - // setWindowState(Qt::WindowFullScreen); - // setCentralWidget(centralWidget); + //setWindowState(Qt::WindowFullScreen); + //setCentralWidget(centralWidget); //todo: ratio - resize(windowWidth, 440); + //resize(windowWidth, 440); setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint); #ifdef DEBUG setWindowTitle(STRING_TITLE); @@ -577,7 +599,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { //setAttribute(Qt::WA_TranslucentBackground, true); ewsUpdateTimer = new QTimer(this); widget = new QWidget(); - layout = new QGridLayout(); + widgetLayout = new QGridLayout(); trayIcon = new QSystemTrayIcon(); trayIconMenu = new QMenu(); trayIconMenuQuit = new QAction(STRING_QUIT); @@ -587,7 +609,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ewsUpdateTimer->setInterval(ewsUpdateTimerFrequency); connect(ewsUpdateTimer, &QTimer::timeout, this, &MainWindow::reorderEndpointWidgetCollection); //widget->setMinimumSize(QSize(300,300)); - widget->setLayout(layout); + widget->setLayout(widgetLayout); /* * Scroll bar code */ @@ -595,6 +617,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { scrollArea->setWidget(widget); scrollArea->setWidgetResizable(true); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + scrollArea->setStyleSheet("QScrollBar:vertical { width: 4px; }"); scrollArea->setMinimumWidth(500); setCentralWidget(scrollArea); @@ -608,12 +632,12 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setMenuBar(mainMenuBar); //setCentralWidget(widget); - //layout->addWidget(pintas, 0, 0); + //widgetLayout->addWidget(pintas, 0, 0); reloadEndpointWidgets(); - scrollArea->setMinimumWidth(ews.at(0)->minimumWidth()); + //scrollArea->setMinimumWidth(ews.at(0)->minimumWidth()); log_debugcpp(std::to_string(scrollArea->minimumWidth())); - + /* * Tray Icon code */ @@ -631,12 +655,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { trayIcon->setToolTip(STRING_TITLE); trayIcon->setContextMenu(trayIconMenu); connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated); - - /* - * Establishing initial window size and position - */ - //TODO: test. hardcode. var. - setGeometry(setSizePosition()); /* * Set of function callback definitons for EndpointSituationCallback @@ -695,15 +713,16 @@ void MainWindow::closeEvent(QCloseEvent *event) { if (trayIcon->isVisible()) { //todo: would be nice to show this to 1st time users; ini-san will come... //this->trayIcon->showMessage("ini file calling","tratarte como un gilipollas la primera vez", QSystemTrayIcon::Information); - hide(); - event->ignore(); + + hide(); + event->ignore(); } } void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Trigger: - this->setGeometry(this->setSizePosition()); + this->calculateChildWidgetsSize(); this->showNormal(); break; default: @@ -721,10 +740,10 @@ void MainWindow::reloadEndpointWidgets() { epwIndex++; //alfinal estoes solopara inicializarlmao ews.push_back(epw); - layout->addWidget(epw, i, 0); + widgetLayout->addWidget(epw, i, 0); } } //todo:: tas aqui tirao, no me gustas y probablemente yo a ti tampoco - layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), i, 0); + widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), i, 0); } diff --git a/src/qt/qtclasses.h b/src/qt/qtclasses.h index 4521120..1098b5c 100644 --- a/src/qt/qtclasses.h +++ b/src/qt/qtclasses.h @@ -1,7 +1,7 @@ #pragma once -#ifndef MAINWINDOW_H -#define MAINWINDOW_H +//#ifndef MAINWINDOW_H +//#define MAINWINDOW_H #include #include @@ -38,31 +38,6 @@ #include "global.h" #include "contclasses.h" -//class EndpointHandler; - -/* - * class ToggleButton : public QAbstractButton { - * Q_OBJECT - * - * public: - * ToggleButton(QWidget *parent = nullptr); - * void checkStateSet(); - * bool hitButton(const QPoint &pos) const; - * void nextCheckState(); - * void changeEvent(QEvent *e) override; - * bool event(QEvent *e) override; - * void focusInEvent(QFocusEvent *e) override; - * void focusOutEvent(QFocusEvent *e) override; - * void keyPressEvent(QKeyEvent *e) override; - * void keyReleaseEvent(QKeyEvent *e) override; - * void mouseMoveEvent(QMouseEvent *e) override; - * void mousePressEvent(QMouseEvent *e) override; - * void mouseReleaseEvent(QMouseEvent *e) override; - * void paintEvent(QPaintEvent *e) override = 0; - * void timerEvent(QTimerEvent *e) override; - * ToggleButton(QWidget *parent = nullptr); - * }; - */ enum SpawnPos { LEFT = (1 << 1), @@ -117,8 +92,7 @@ private: QLabel *mainLabel = nullptr; QSlider *mainSlider = nullptr; uint64_t idx; - //QGridLayout *layout = nullptr; - QHBoxLayout *layout = nullptr; + QHBoxLayout *widgetLayout = nullptr; QCheckBox *muteButton = nullptr; SessionHandler* sh; QTimer* volumePoller = nullptr; @@ -137,7 +111,7 @@ private: uint32_t channelCount; std::vector channelSliders; std::vector channelLabels; - QGridLayout *layout; + QGridLayout *widgetLayout; }; @@ -181,7 +155,7 @@ private: QSlider *mainSlider = nullptr; std::vector channelSliders; std::vector channelLabels; - QGridLayout *layout = nullptr; + QGridLayout *widgetLayout = nullptr; QGridLayout *mainMuteLayout = nullptr; std::map defaultRolesCheckBoxes; @@ -205,25 +179,8 @@ Q_OBJECT public: HeaderWidget(QWidget *parent = nullptr); - //~HeaderWidget(); - //void updateMainVolume(float newValue); - //void updateVolume(uint32_t channel, float newValue); - //void updateMute(bool muted); - - //void populateEndpointWidget(EndpointHandler *eph); - //void setEndpointHandlers(std::vector *ephs); - -//public slots: - -//protected: - //void customEvent(QEvent* ev) override; - -//private slots: - //void addSessionWidget(CustomWidgetEvent* ev); - //void removeSessionWidget(CustomWidgetEvent* ev); - private: - QGridLayout *layout; + QGridLayout *widgetLayout; QPushButton *about; #ifdef WIN32 QPushButton *openCP; @@ -238,11 +195,12 @@ class MainWindow : public QMainWindow { public: MainWindow(QWidget *parent = nullptr); void reloadEndpointWidgets(); + void calculateChildWidgetsSize(); protected: void closeEvent(QCloseEvent *event) override; void customEvent(QEvent* ev) override; - QRect setSizePosition(); + QRect setSizePosition(int width, int height); private slots: void trayIconActivated(QSystemTrayIcon::ActivationReason reason); @@ -256,7 +214,7 @@ private: //std::vector *ephs; std::vector ews; QWidget *widget; - QGridLayout *layout; + QGridLayout *widgetLayout; QSystemTrayIcon *trayIcon; QMenu *trayIconMenu; @@ -283,4 +241,4 @@ private: }; -#endif +//#endif diff --git a/src/qtestmain.cpp b/src/qtestmain.cpp index 85e1928..a1fbeea 100644 --- a/src/qtestmain.cpp +++ b/src/qtestmain.cpp @@ -66,8 +66,10 @@ int main (int argc, char* argv[]) { */ //app->setStyleSheet(styleSheet); - //window.setMinimumSize(100, 100); + window.calculateChildWidgetsSize(); + #ifdef DEBUG window.show(); + #endif return app->exec(); }