Add DynamicGridWidget

This commit is contained in:
Florian Märkl 2019-06-28 10:58:10 +02:00
commit 0e547f1540
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 166 additions and 1 deletions

View file

@ -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)

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_DYNAMICGRIDWIDGET_H
#define CHIAKI_DYNAMICGRIDWIDGET_H
#include <QWidget>
class QGridLayout;
class DynamicGridWidget : public QWidget
{
Q_OBJECT
private:
QGridLayout *layout;
QList<QWidget *> 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

View file

@ -20,10 +20,16 @@
#include <QWidget>
class QTableWidget;
class DynamicGridWidget;
class MainWindow : public QWidget
{
Q_OBJECT
private:
DynamicGridWidget *grid_widget;
public:
explicit MainWindow(QWidget *parent = nullptr);
};

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <dynamicgridwidget.h>
#include <QGridLayout>
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; i<widgets.length(); i++)
layout->addWidget(widgets[i], i / columns, i % columns);
setMinimumWidth(item_width);
}
void DynamicGridWidget::UpdateLayoutIfNecessary()
{
unsigned int new_columns = CalculateColumns();
if(new_columns != columns)
UpdateLayout();
}

View file

@ -16,7 +16,39 @@
*/
#include <mainwindow.h>
#include <dynamicgridwidget.h>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QScrollArea>
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);
}