From 142cbf5ea03bc035ee5c89a0b7f5f1009c10eeee Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 28 Feb 2020 02:12:18 +0100 Subject: [PATCH] Add PostWidget and put some test posts into the QTreeWidget. --- gui/FediPotato.qrc | 1 + gui/images/no_avatar.svg | 94 ++++++++++++++++++++++++++++++++++++++++ gui/src/widget_post.cpp | 34 +++++++++++++++ gui/src/widget_post.hpp | 66 ++++++++++++++++++++++++++++ gui/src/window_main.cpp | 47 ++++++++++++++++++++ gui/ui/widget_post.ui | 54 ++++++++++++++++++----- gui/ui/window_main.ui | 16 ++++++- 7 files changed, 299 insertions(+), 13 deletions(-) create mode 100644 gui/images/no_avatar.svg create mode 100644 gui/src/widget_post.cpp create mode 100644 gui/src/widget_post.hpp diff --git a/gui/FediPotato.qrc b/gui/FediPotato.qrc index 8df7545..afb68d0 100644 --- a/gui/FediPotato.qrc +++ b/gui/FediPotato.qrc @@ -2,5 +2,6 @@ ../AUTHORS + images/no_avatar.svg diff --git a/gui/images/no_avatar.svg b/gui/images/no_avatar.svg new file mode 100644 index 0000000..006a1f6 --- /dev/null +++ b/gui/images/no_avatar.svg @@ -0,0 +1,94 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/gui/src/widget_post.cpp b/gui/src/widget_post.cpp new file mode 100644 index 0000000..e4dd006 --- /dev/null +++ b/gui/src/widget_post.cpp @@ -0,0 +1,34 @@ +/* This file is part of FediPotato. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "widget_post.hpp" + +namespace FediPotato +{ + +void PostWidget::show_hide_elements() +{ + const bool has_subject{label_subject->text().isEmpty()}; + label_subject->setHidden(has_subject); + button_more->setHidden(has_subject); + + if (label_avatar->pixmap() == nullptr) + { + set_avatar(":/images/no_avatar.svg"); + } +} + +} // namespace FediPotato diff --git a/gui/src/widget_post.hpp b/gui/src/widget_post.hpp new file mode 100644 index 0000000..efe76bf --- /dev/null +++ b/gui/src/widget_post.hpp @@ -0,0 +1,66 @@ +#ifndef FEDIPOTATO_GUI_WIDGET_POST_HPP +#define FEDIPOTATO_GUI_WIDGET_POST_HPP + +#include "../ui/ui_widget_post.h" + +#include +#include +#include +#include +#include + +namespace FediPotato +{ + +class PostWidget : public QWidget, private Ui::PostWidget +{ +public: + //! Default constructor + explicit PostWidget(QWidget *parent = nullptr) + : QWidget{parent} + { + setupUi(this); + } + + //! Copy constructor + PostWidget(const PostWidget &other) = delete; + + //! Move constructor + PostWidget(PostWidget &&other) noexcept = delete; + + //! Destructor + ~PostWidget() noexcept override = default; + + //! Copy assignment operator + PostWidget& operator=(const PostWidget &other) = delete; + + //! Move assignment operator + PostWidget& operator=(PostWidget &&other) noexcept = delete; + +public: + inline void set_user(const QString &user) + { + label_user->setText(user); + } + + inline void set_avatar(const QString &avatar) + { + label_avatar->setPixmap(QIcon(avatar).pixmap(QSize(50, 50))); + } + + inline void set_subject(const QString &subject) + { + label_subject->setText(subject); + } + + inline void set_content(const QString &content) + { + label_content->setText(content); + } + + void show_hide_elements(); +}; + +} // namespace FediPotato + +#endif // FEDIPOTATO_GUI_WIDGET_POST_HPP diff --git a/gui/src/window_main.cpp b/gui/src/window_main.cpp index 3598be9..b8e73a0 100644 --- a/gui/src/window_main.cpp +++ b/gui/src/window_main.cpp @@ -17,6 +17,7 @@ #include "window_main.hpp" #include "channel.hpp" #include "dialog_about.hpp" +#include "widget_post.hpp" #include #include @@ -33,7 +34,53 @@ MainWindow::MainWindow(QMainWindow *parent) { setupUi(this); + // Add account for testing. add_account(); + + // Add some post widgets for testing. + // FIXME: The widgets don't resize properly. + QTreeWidgetItem *item_root{treewidget_posts->invisibleRootItem()}; + for (int i{0}; i < 2; ++i) + { + auto *pw1{new PostWidget(treewidget_posts)}; + pw1->set_user("@user@example.com"); + pw1->set_subject("Food"); + pw1->set_content("I ate a whole lot of potatos today!"); + pw1->set_avatar(":/images/no_avatar.svg"); + pw1->show_hide_elements(); + auto *item1{new QTreeWidgetItem}; + item_root->addChild(item1); + treewidget_posts->setItemWidget(item1, 0, pw1); + + auto *pw2{new PostWidget(treewidget_posts)}; + pw2->set_user("@user@example.com"); + pw2->set_subject("Pseudo-latin"); + pw2->set_content("Aliquam feugiat tellus ut neque. Sed bibendum. " + "Mauris ac felis vel velit tristique imperdiet."); + pw2->show_hide_elements(); + auto *item2{new QTreeWidgetItem}; + item1->addChild(item2); + treewidget_posts->setItemWidget(item2, 0, pw2); + + auto *pw4{new PostWidget(treewidget_posts)}; + pw4->set_user("@user@example.com"); + pw4->set_content("Nam vestibulum accumsan nisl. " + "Praesent fermentum tempor tellus. Fusce commodo. " + "Phasellus purus. " + "Praesent fermentum tempor tellus."); + pw4->show_hide_elements(); + auto *item4{new QTreeWidgetItem}; + item2->addChild(item4); + treewidget_posts->setItemWidget(item4, 0, pw4); + + auto *pw3{new PostWidget(treewidget_posts)}; + pw3->set_user("@user@example.com"); + pw3->set_content("👍"); + pw3->show_hide_elements(); + auto *item3{new QTreeWidgetItem}; + item1->addChild(item3); + treewidget_posts->setItemWidget(item3, 0, pw3); + } } void MainWindow::add_account() diff --git a/gui/ui/widget_post.ui b/gui/ui/widget_post.ui index 2430cc3..5e058e6 100644 --- a/gui/ui/widget_post.ui +++ b/gui/ui/widget_post.ui @@ -6,24 +6,36 @@ 0 0 - 250 - 130 + 200 + 115 - + 0 0 + + + 150 + 50 + + Form + + background-color: blue; + + + QLayout::SetDefaultConstraint + - QLayout::SetDefaultConstraint + QLayout::SetFixedSize @@ -38,14 +50,14 @@ - 100 - 100 + 50 + 50 - 100 - 100 + 50 + 50 @@ -82,10 +94,13 @@ + + QLayout::SetMinimumSize + - + 0 0 @@ -96,6 +111,9 @@ true + + background-color: green; + @@ -116,11 +134,14 @@ - + 0 0 + + background-color: green; + @@ -137,6 +158,12 @@ + + + 0 + 0 + + Show more @@ -148,7 +175,7 @@ - + 0 0 @@ -159,6 +186,9 @@ false + + background-color: green; + QFrame::NoFrame @@ -166,7 +196,7 @@ - Qt::RichText + Qt::PlainText Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop diff --git a/gui/ui/window_main.ui b/gui/ui/window_main.ui index 6a8a834..8bd65cb 100644 --- a/gui/ui/window_main.ui +++ b/gui/ui/window_main.ui @@ -37,13 +37,27 @@ - + QAbstractItemView::NoEditTriggers + + QAbstractItemView::NoSelection + + + 20 + + + true + false + + + 1 + +