From 0ea443025258aacd40dc5b50d6c136913ea7e189 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Sun, 22 Mar 2015 19:41:59 +0100 Subject: [PATCH] playing around with qt to find the correct ui approach --- editorelement.cpp | 26 +++++++ editorelement.h | 28 +++++++ flowlayout.cpp | 150 ++++++++++++++++++++++++++++++++++++++ flowlayout.h | 37 ++++++++++ main.cpp | 11 +++ mainwindow.cpp | 27 +++++++ mainwindow.h | 23 ++++++ networkpacketcomposer.pro | 25 +++++++ 8 files changed, 327 insertions(+) create mode 100644 editorelement.cpp create mode 100644 editorelement.h create mode 100644 flowlayout.cpp create mode 100644 flowlayout.h create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 networkpacketcomposer.pro diff --git a/editorelement.cpp b/editorelement.cpp new file mode 100644 index 0000000..1952beb --- /dev/null +++ b/editorelement.cpp @@ -0,0 +1,26 @@ +#include "editorelement.h" +#include + +EditorElement::EditorElement(QWidget *parent) : QWidget(parent) +{ + this->layout = new QBoxLayout(QBoxLayout::TopToBottom, this); + this->setLayout(layout); + label = new QLabel(this); + hint = new QLabel(this); + edit = new QLineEdit(this); + layout->addWidget(label); + layout->addWidget(hint); + layout->addWidget(edit); + + label->setText("test"); + hint->setText("another test"); + edit->setText("content test"); + + +} + +EditorElement::~EditorElement() +{ + +} + diff --git a/editorelement.h b/editorelement.h new file mode 100644 index 0000000..7df81dd --- /dev/null +++ b/editorelement.h @@ -0,0 +1,28 @@ +#ifndef EDITORELEMENT_H +#define EDITORELEMENT_H +#include +#include +#include +#include +#include +#include + +class EditorElement : public QWidget +{ + Q_OBJECT +public: + explicit EditorElement(QWidget *parent = 0); + ~EditorElement(); +private: + QBoxLayout*layout; + QLabel * label; + QLabel * hint; + QLineEdit* edit; + + +signals: + +public slots: +}; + +#endif // EDITORELEMENT_H diff --git a/flowlayout.cpp b/flowlayout.cpp new file mode 100644 index 0000000..6e3f252 --- /dev/null +++ b/flowlayout.cpp @@ -0,0 +1,150 @@ +#include + +#include "flowlayout.h" +FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) + : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) +{ + setContentsMargins(margin, margin, margin, margin); +} + +FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) + : m_hSpace(hSpacing), m_vSpace(vSpacing) +{ + setContentsMargins(margin, margin, margin, margin); +} + +FlowLayout::~FlowLayout() +{ + QLayoutItem *item; + while ((item = takeAt(0))) + delete item; +} + +void FlowLayout::addItem(QLayoutItem *item) +{ + itemList.append(item); +} + +int FlowLayout::horizontalSpacing() const +{ + if (m_hSpace >= 0) { + return m_hSpace; + } else { + return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); + } +} + +int FlowLayout::verticalSpacing() const +{ + if (m_vSpace >= 0) { + return m_vSpace; + } else { + return smartSpacing(QStyle::PM_LayoutVerticalSpacing); + } +} + +int FlowLayout::count() const +{ + return itemList.size(); +} + +QLayoutItem *FlowLayout::itemAt(int index) const +{ + return itemList.value(index); +} + +QLayoutItem *FlowLayout::takeAt(int index) +{ + if (index >= 0 && index < itemList.size()) + return itemList.takeAt(index); + else + return 0; +} + +Qt::Orientations FlowLayout::expandingDirections() const +{ + return 0; +} + +bool FlowLayout::hasHeightForWidth() const +{ + return true; +} + +int FlowLayout::heightForWidth(int width) const +{ + int height = doLayout(QRect(0, 0, width, 0), true); + return height; +} + +void FlowLayout::setGeometry(const QRect &rect) +{ + QLayout::setGeometry(rect); + doLayout(rect, false); +} + +QSize FlowLayout::sizeHint() const +{ + return minimumSize(); +} + +QSize FlowLayout::minimumSize() const +{ + QSize size; + QLayoutItem *item; + foreach (item, itemList) + size = size.expandedTo(item->minimumSize()); + + size += QSize(2*margin(), 2*margin()); + return size; +} + +int FlowLayout::doLayout(const QRect &rect, bool testOnly) const +{ + int left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); + int x = effectiveRect.x(); + int y = effectiveRect.y(); + int lineHeight = 0; + + QLayoutItem *item; + foreach (item, itemList) { + QWidget *wid = item->widget(); + int spaceX = horizontalSpacing(); + if (spaceX == -1) + spaceX = wid->style()->layoutSpacing( + QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal); + int spaceY = verticalSpacing(); + if (spaceY == -1) + spaceY = wid->style()->layoutSpacing( + QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); + int nextX = x + item->sizeHint().width() + spaceX; + if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { + x = effectiveRect.x(); + y = y + lineHeight + spaceY; + nextX = x + item->sizeHint().width() + spaceX; + lineHeight = 0; + } + + if (!testOnly) + item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); + + x = nextX; + lineHeight = qMax(lineHeight, item->sizeHint().height()); + } + return y + lineHeight - rect.y() + bottom; +} +int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const +{ + QObject *parent = this->parent(); + if (!parent) { + return -1; + } else if (parent->isWidgetType()) { + QWidget *pw = static_cast(parent); + return pw->style()->pixelMetric(pm, 0, pw); + } else { + return static_cast(parent)->spacing(); + } +} + diff --git a/flowlayout.h b/flowlayout.h new file mode 100644 index 0000000..cd22722 --- /dev/null +++ b/flowlayout.h @@ -0,0 +1,37 @@ +#ifndef FLOWLAYOUT_H +#define FLOWLAYOUT_H + + +#include +#include +#include +class FlowLayout : public QLayout +{ +public: + explicit FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1); + explicit FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1); + ~FlowLayout(); + + void addItem(QLayoutItem *item) Q_DECL_OVERRIDE; + int horizontalSpacing() const; + int verticalSpacing() const; + Qt::Orientations expandingDirections() const Q_DECL_OVERRIDE; + bool hasHeightForWidth() const Q_DECL_OVERRIDE; + int heightForWidth(int) const Q_DECL_OVERRIDE; + int count() const Q_DECL_OVERRIDE; + QLayoutItem *itemAt(int index) const Q_DECL_OVERRIDE; + QSize minimumSize() const Q_DECL_OVERRIDE; + void setGeometry(const QRect &rect) Q_DECL_OVERRIDE; + QSize sizeHint() const Q_DECL_OVERRIDE; + QLayoutItem *takeAt(int index) Q_DECL_OVERRIDE; + +private: + int doLayout(const QRect &rect, bool testOnly) const; + int smartSpacing(QStyle::PixelMetric pm) const; + + QList itemList; + int m_hSpace; + int m_vSpace; +}; + +#endif // FLOWLAYOUT_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a080bdd --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..80dda28 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,27 @@ +#include "mainwindow.h" +#include "../QtGuidedEditor/qgewidget.h" +#include "flowlayout.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + list = new QList(); + editor = new QWidget(this); + centralLayout = new FlowLayout(editor); + editor->setLayout(centralLayout); + centralLayout->addWidget(new EditorElement(this)); + centralLayout->addWidget(new EditorElement(this)); + this->setCentralWidget(editor); + QString style = QString( + "QWidget {border: 2px dashed black;" + "border-radius: 5px;" + "background: gray }" + ); + editor->setStyleSheet(style); + +} + +MainWindow::~MainWindow() +{ + +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..11ed6cc --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,23 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include "editorelement.h" +#include "flowlayout.h" + +class MainWindow : public QMainWindow +{ + Q_OBJECT +private: + FlowLayout* centralLayout; + QWidget* editor; + QList* list; + + +public: + MainWindow(QWidget *parent = 0); + ~MainWindow(); +}; + +#endif // MAINWINDOW_H diff --git a/networkpacketcomposer.pro b/networkpacketcomposer.pro new file mode 100644 index 0000000..bb118d2 --- /dev/null +++ b/networkpacketcomposer.pro @@ -0,0 +1,25 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-01-06T11:56:33 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = networkpacketcomposer +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + editorelement.cpp \ + flowlayout.cpp + +HEADERS += mainwindow.h \ + editorelement.h \ + flowlayout.h + +INCLUDEPATH += $$PWD/../QtGuidedEditor +DEPENDPATH += $$PWD/../QtGuidedEditor