Skip to content

Commit

Permalink
Reduced font size, added size hint and more.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjell Morgenstern authored and KjellMorgenstern committed Dec 12, 2023
1 parent 60e26b4 commit 7f78497
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
47 changes: 33 additions & 14 deletions src/sketch/welcomeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,47 +126,52 @@ CustomListItem::CustomListItem(const QString &leftText, const QIcon &leftIcon, c
int listWidgetWidth, QWidget *parent)
: QWidget(parent), leftData(leftData), rightData(rightData) {
QHBoxLayout *layout = new QHBoxLayout(this);
int padding = 5;
int padding = 3;
layout->setContentsMargins(2, 2, 2, 2);
layout->setSpacing(0);

QList<QSize> availableIconSizes = leftIcon.availableSizes();
m_iconSize = availableIconSizes.isEmpty() ? QSize(16, 16) : availableIconSizes.first();

leftButton = new QPushButton(leftIcon, "", this);
rightButton = new QPushButton(rightIcon, "", this);

leftButton->setFlat(true);
rightButton->setFlat(true);

QString buttonStyle = QString("QPushButton { "
"text-align: left; "
"background-color: transparent; "
"border: none; "
"padding: %1px; "
"padding-left: %1px; "
"padding-right: %1px; "
"}"
"QPushButton:pressed { "
"color: #555; "
"}").arg(padding);
leftButton->setStyleSheet(buttonStyle);
rightButton->setStyleSheet(buttonStyle);

QFont buttonFont = leftButton->font();
buttonFont.setPointSize(buttonFont.pointSize() - 2);
leftButton->setFont(buttonFont);
rightButton->setFont(buttonFont);

QPalette buttonPalette = leftButton->palette();
buttonPalette.setColor(QPalette::ButtonText, Qt::black);
leftButton->setPalette(buttonPalette);
rightButton->setPalette(buttonPalette);

int scrollbarWidth = this->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
int leftButtonWidth = static_cast<int>((listWidgetWidth - scrollbarWidth) * 0.5);
int rightButtonWidth = static_cast<int>((listWidgetWidth - scrollbarWidth) * 0.5);
int leftButtonWidth = static_cast<int>((listWidgetWidth - scrollbarWidth) * 0.75);
int rightButtonWidth = static_cast<int>((listWidgetWidth - scrollbarWidth) * 0.25);

leftButton->setFixedWidth(leftButtonWidth);
rightButton->setFixedWidth(rightButtonWidth);

QFontMetrics metrics(leftButton->font());
int buttonHeight = metrics.height() + 2 * padding;

leftButton->setMinimumHeight(buttonHeight);
rightButton->setMinimumHeight(buttonHeight);

auto leftIconSizes = leftIcon.availableSizes();
int leftIconWidth = leftIconSizes.isEmpty() ? 0 : leftIconSizes.first().width();
QString elidedLeftText = metrics.elidedText(leftText, Qt::ElideRight, leftButtonWidth - leftIconWidth - 3 * padding);
QString elidedRightText = metrics.elidedText(rightText, Qt::ElideRight, rightButtonWidth - leftIconWidth - 3 * padding);
QString elidedLeftText = metrics.elidedText(leftText, Qt::ElideRight, leftButtonWidth - m_iconSize.width() - 3 * padding);
QString elidedRightText = metrics.elidedText(rightText, Qt::ElideRight, rightButtonWidth - m_iconSize.width() - 3 * padding);

leftButton->setText(elidedLeftText);
leftButton->setToolTip(leftData);
Expand All @@ -182,6 +187,20 @@ CustomListItem::CustomListItem(const QString &leftText, const QIcon &leftIcon, c
setLayout(layout);
}

QSize CustomListItem::sizeHint() const {
QFontMetrics metrics(font());
int textHeight = metrics.height();

int verticalPadding = 10;
int totalHeight = qMax(textHeight, m_iconSize.height()) + verticalPadding;

// Width is based on the list widget's width
int listWidgetWidth = parentWidget() ? parentWidget()->width() : 100;
int totalWidth = listWidgetWidth - layout()->contentsMargins().left() - layout()->contentsMargins().right();

return QSize(totalWidth, totalHeight);
}

void CustomListItem::onLeftButtonClicked() {
emit leftItemClicked(leftData);
}
Expand Down Expand Up @@ -766,7 +785,7 @@ void WelcomeView::updateRecent() {
if (link.endsWith(QChar('/'))) {
link.chop(1); // Remove the last character
}
rightText = QString("%1 Project").arg(fabName);
rightText = QString("%1").arg(fabName);
rightData = link; // Data for the right button click
QPixmap pixmap = SqliteReferenceModel().retrieveIcon(fabName);
if (!pixmap.isNull()) {
Expand Down
26 changes: 14 additions & 12 deletions src/sketch/welcomeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,28 @@ along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
#include <QHBoxLayout>

class CustomListItem : public QWidget {
Q_OBJECT
Q_OBJECT

public:
explicit CustomListItem(const QString &leftText, const QIcon &leftIcon, const QString &leftData,
const QString &rightText, const QIcon &rightIcon, const QString &rightData,
int listWidgetWidth, QWidget *parent = nullptr);
explicit CustomListItem(const QString &leftText, const QIcon &leftIcon, const QString &leftData,
const QString &rightText, const QIcon &rightIcon, const QString &rightData,
int listWidgetWidth, QWidget *parent = nullptr);
QSize sizeHint() const;

signals:
void leftItemClicked(const QString &data);
void rightItemClicked(const QString &data);
void leftItemClicked(const QString &data);
void rightItemClicked(const QString &data);

private slots:
void onLeftButtonClicked();
void onRightButtonClicked();
void onLeftButtonClicked();
void onRightButtonClicked();

private:
QPushButton *leftButton;
QPushButton *rightButton;
QString leftData;
QString rightData;
QPushButton *leftButton;
QPushButton *rightButton;
QString leftData;
QString rightData;
QSize m_iconSize;
};

class BlogListWidget : public QListWidget
Expand Down

0 comments on commit 7f78497

Please sign in to comment.