From 0e547f154008c6e46b4754b65adfb55d7f63fe81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 28 Jun 2019 10:58:10 +0200 Subject: [PATCH] Add DynamicGridWidget --- gui/CMakeLists.txt | 4 +- gui/include/dynamicgridwidget.h | 52 +++++++++++++++++++++++ gui/include/mainwindow.h | 6 +++ gui/src/dynamicgridwidget.cpp | 73 +++++++++++++++++++++++++++++++++ gui/src/mainwindow.cpp | 32 +++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 gui/include/dynamicgridwidget.h create mode 100644 gui/src/dynamicgridwidget.cpp diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 88c218d..acf07ce 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -16,7 +16,9 @@ add_executable(chiaki include/discoverycmd.h src/discoverycmd.cpp include/mainwindow.h - src/mainwindow.cpp) + src/mainwindow.cpp + include/dynamicgridwidget.h + src/dynamicgridwidget.cpp) target_include_directories(chiaki PRIVATE include) target_link_libraries(chiaki chiaki-lib) diff --git a/gui/include/dynamicgridwidget.h b/gui/include/dynamicgridwidget.h new file mode 100644 index 0000000..30c6c27 --- /dev/null +++ b/gui/include/dynamicgridwidget.h @@ -0,0 +1,52 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_DYNAMICGRIDWIDGET_H +#define CHIAKI_DYNAMICGRIDWIDGET_H + +#include + +class QGridLayout; + +class DynamicGridWidget : public QWidget +{ + Q_OBJECT + + private: + QGridLayout *layout; + QList widgets; + unsigned int item_width; + unsigned int columns; + + void UpdateLayout(); + void UpdateLayoutIfNecessary(); + unsigned int CalculateColumns(); + + protected: + void resizeEvent(QResizeEvent *) override { UpdateLayoutIfNecessary(); } + + public: + explicit DynamicGridWidget(unsigned int item_width, QWidget *parent = nullptr); + + void AddWidget(QWidget *widget); + void RemoveWidget(QWidget *widget); + + void SetItemWidth(int item_width) { this->item_width = item_width; UpdateLayoutIfNecessary(); } + +}; + +#endif //CHIAKI_DYNAMICGRIDWIDGET_H diff --git a/gui/include/mainwindow.h b/gui/include/mainwindow.h index 06a92a7..26318ed 100644 --- a/gui/include/mainwindow.h +++ b/gui/include/mainwindow.h @@ -20,10 +20,16 @@ #include +class QTableWidget; +class DynamicGridWidget; + class MainWindow : public QWidget { Q_OBJECT + private: + DynamicGridWidget *grid_widget; + public: explicit MainWindow(QWidget *parent = nullptr); }; diff --git a/gui/src/dynamicgridwidget.cpp b/gui/src/dynamicgridwidget.cpp new file mode 100644 index 0000000..443a87e --- /dev/null +++ b/gui/src/dynamicgridwidget.cpp @@ -0,0 +1,73 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +#include + +DynamicGridWidget::DynamicGridWidget(unsigned int item_width, QWidget *parent) +{ + layout = new QGridLayout(this); + layout->setHorizontalSpacing(10); + layout->setVerticalSpacing(10); + setLayout(layout); + this->item_width = item_width; + columns = CalculateColumns(); +} + +void DynamicGridWidget::AddWidget(QWidget *widget) +{ + if(widgets.contains(widget)) + return; + widget->setParent(this); + widgets.append(widget); + UpdateLayout(); +} + +void DynamicGridWidget::RemoveWidget(QWidget *widget) +{ + layout->removeWidget(widget); + widget->setParent(nullptr); + widgets.removeAll(widget); +} + +unsigned int DynamicGridWidget::CalculateColumns() +{ + return (width() + layout->horizontalSpacing()) / (item_width + layout->horizontalSpacing()); +} + +void DynamicGridWidget::UpdateLayout() +{ + while(layout->count() > 0) + layout->removeItem(layout->itemAt(0)); + + columns = CalculateColumns(); + if(columns == 0) + return; + + for(unsigned int i=0; iaddWidget(widgets[i], i / columns, i % columns); + + setMinimumWidth(item_width); +} + +void DynamicGridWidget::UpdateLayoutIfNecessary() +{ + unsigned int new_columns = CalculateColumns(); + if(new_columns != columns) + UpdateLayout(); +} diff --git a/gui/src/mainwindow.cpp b/gui/src/mainwindow.cpp index 9e15964..8f07051 100644 --- a/gui/src/mainwindow.cpp +++ b/gui/src/mainwindow.cpp @@ -16,7 +16,39 @@ */ #include +#include + +#include +#include +#include MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { + auto layout = new QVBoxLayout(); + setLayout(layout); + + auto scroll_area = new QScrollArea(this); + scroll_area->setWidgetResizable(true); + scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + layout->addWidget(scroll_area); + + auto scroll_content_widget = new QWidget(scroll_area); + auto scroll_content_layout = new QVBoxLayout(scroll_content_widget); + scroll_content_widget->setLayout(scroll_content_layout); + scroll_area->setWidget(scroll_content_widget); + + grid_widget = new DynamicGridWidget(100, scroll_content_widget); + scroll_content_layout->addWidget(grid_widget); + scroll_content_layout->addStretch(0); + grid_widget->setContentsMargins(0, 0, 0, 0); + + for(int i=0; i<10; i++) + { + QWidget *w = new QWidget(grid_widget); + w->setFixedSize(100, 100); + w->setStyleSheet("background-color: red;"); + grid_widget->AddWidget(w); + } + + resize(800, 600); }