Skip to content

Commit

Permalink
WIP: Working on proper sizing and layout of the tags display
Browse files Browse the repository at this point in the history
  • Loading branch information
droidmonkey committed Dec 23, 2024
1 parent 571f7ba commit e7553ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
27 changes: 21 additions & 6 deletions src/gui/EntryPreviewWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>530</width>
<height>257</height>
<height>296</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_7">
Expand Down Expand Up @@ -260,8 +260,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>157</width>
<height>63</height>
<width>152</width>
<height>57</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_8">
Expand Down Expand Up @@ -481,13 +481,28 @@
</layout>
</item>
<item row="2" column="1" colspan="5">
<widget class="TagsEdit" name="entryTagsList" native="true">
<widget class="TagsEdit" name="entryTagsList">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>80</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
<enum>Qt::NoFocus</enum>
</property>
<property name="accessibleName">
<string>Tags list</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
</widget>
</item>
<item row="1" column="5">
Expand Down Expand Up @@ -1218,7 +1233,7 @@
</customwidget>
<customwidget>
<class>TagsEdit</class>
<extends>QWidget</extends>
<extends>QScrollArea</extends>
<header>gui/tag/TagsEdit.h</header>
<container>1</container>
</customwidget>
Expand Down
6 changes: 6 additions & 0 deletions src/gui/entry/EditEntryWidgetMain.ui
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
</item>
<item row="5" column="1">
<widget class="TagsEdit" name="tagsList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
Expand Down
47 changes: 15 additions & 32 deletions src/gui/tag/TagsEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ struct TagsEdit::Impl
}

void insertText(const QString& text) {
currentText().insert(cursor, text);
Q_ASSERT(tags.editingIndex() != tags.end());
tags.editingIndex()->text.insert(cursor, text);
moveCursor(cursor + text.size(), false);
}

Expand Down Expand Up @@ -368,7 +369,7 @@ struct TagsEdit::Impl
void setCurrentText(const QString& text)
{
Q_ASSERT(tags.editingIndex() != tags.end());
currentText() = text;
tags.editingIndex()->text = text;
moveCursor(currentText().length(), false);
updateDisplayText();
calcRectsAndUpdateScrollRanges();
Expand All @@ -391,7 +392,7 @@ struct TagsEdit::Impl
// and ensures Invariant-1.
void editNewTag(const iterator& i)
{
currentText() = currentText().trimmed();
tags.editingIndex()->text = currentText().trimmed();
auto inserted_at = tags.insert(i, Tag());
setEditingIndex(inserted_at);
moveCursor(0, false);
Expand Down Expand Up @@ -419,7 +420,7 @@ struct TagsEdit::Impl
void removeSelection()
{
cursor = select_start;
currentText().remove(cursor, select_size);
tags.editingIndex()->text.remove(cursor, select_size);
deselectAll();
}

Expand All @@ -428,7 +429,7 @@ struct TagsEdit::Impl
if (hasSelection()) {
removeSelection();
} else {
currentText().remove(--cursor, 1);
tags.editingIndex()->text.remove(--cursor, 1);
}
}

Expand Down Expand Up @@ -618,7 +619,6 @@ struct TagsEdit::Impl
int select_start;
int select_size;
bool cross_deleter;
int hscroll{0};
QTextLayout text_layout;

public:
Expand All @@ -641,11 +641,8 @@ struct TagsEdit::Impl
const auto r = currentRect();
const auto txt_p = r.topLeft() + QPoint(TAG_INNER.left(), ((r.height() - fontHeight) / 2));

// Nothing to draw. Don't draw anything to avoid adding text margins.
if (!it->isEmpty()) {
// draw not terminated tag
text_layout.draw(&p, txt_p - scrollOffsets, formatting());
}
// draw not terminated tag
text_layout.draw(&p, txt_p - scrollOffsets, formatting());

// draw cursor
if (drawCursor) {
Expand All @@ -663,19 +660,8 @@ TagsEdit::TagsEdit(QWidget* parent)
, impl(new Impl(this))
, completer(new QCompleter)
{
QSizePolicy size_policy(QSizePolicy::Ignored, QSizePolicy::Preferred);
size_policy.setHeightForWidth(true);
setSizePolicy(size_policy);

setFocusPolicy(Qt::StrongFocus);
viewport()->setCursor(Qt::IBeamCursor);
setAttribute(Qt::WA_InputMethodEnabled, true);
setMouseTracking(true);

setReadOnly(false);
setupCompleter();
setCursorVisible(hasFocus());
impl->updateDisplayText();

viewport()->setContentsMargins(TAG_H_SPACING, TAG_V_SPACING, TAG_H_SPACING, TAG_V_SPACING);
}

Expand All @@ -694,6 +680,7 @@ void TagsEdit::setReadOnly(bool readOnly)
setCursor(Qt::IBeamCursor);
setAttribute(Qt::WA_InputMethodEnabled, true);
}
setMouseTracking(!m_readOnly);
impl->setReadOnly(m_readOnly);
}

Expand Down Expand Up @@ -837,24 +824,20 @@ void TagsEdit::mousePressEvent(QMouseEvent* event)
}
}

QSize TagsEdit::sizeHint() const
QSize TagsEdit::viewportSizeHint() const
{
return minimumSizeHint();
return impl->updateTagRenderStates({0, 0, width(), 0}).size();
}

QSize TagsEdit::minimumSizeHint() const
bool TagsEdit::hasHeightForWidth() const
{
ensurePolished();
QFontMetrics fm = fontMetrics();
QRect rect(0, 0, fm.maxWidth() + TAG_CROSS_PADDING + TAG_CROSS_WIDTH, fm.height() + fm.leading());
rect += TAG_INNER + contentsMargins() + viewport()->contentsMargins() + viewportMargins();
return rect.size();
return true;
}

int TagsEdit::heightForWidth(int w) const
{
const auto content_width = w;
QRect contents_rect(0, 0, content_width, 100);
QRect contents_rect(0, 0, content_width, 32);
contents_rect -= contentsMargins() + viewport()->contentsMargins() + viewportMargins();
contents_rect = impl->updateTagRenderStates(contents_rect);
contents_rect += contentsMargins() + viewport()->contentsMargins() + viewportMargins();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/tag/TagsEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class TagsEdit : public QAbstractScrollArea
explicit TagsEdit(QWidget* parent = nullptr);
~TagsEdit() override;

QSize sizeHint() const override;
QSize minimumSizeHint() const override;
bool hasHeightForWidth() const override;
int heightForWidth(int w) const override;

void setReadOnly(bool readOnly);
Expand All @@ -60,6 +59,7 @@ class TagsEdit : public QAbstractScrollArea
void keyPressEvent(QKeyEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void hideEvent(QHideEvent* event) override;
QSize viewportSizeHint() const override;

private:
bool isAcceptableInput(QKeyEvent const* event) const;
Expand Down

0 comments on commit e7553ea

Please sign in to comment.