@@ -339,7 +339,8 @@ struct TagsEdit::Impl
339339 }
340340
341341 void insertText (const QString& text) {
342- currentText ().insert (cursor, text);
342+ Q_ASSERT (tags.editingIndex () != tags.end ());
343+ tags.editingIndex ()->text .insert (cursor, text);
343344 moveCursor (cursor + text.size (), false );
344345 }
345346
@@ -368,7 +369,7 @@ struct TagsEdit::Impl
368369 void setCurrentText (const QString& text)
369370 {
370371 Q_ASSERT (tags.editingIndex () != tags.end ());
371- currentText () = text;
372+ tags. editingIndex ()-> text = text;
372373 moveCursor (currentText ().length (), false );
373374 updateDisplayText ();
374375 calcRectsAndUpdateScrollRanges ();
@@ -391,7 +392,7 @@ struct TagsEdit::Impl
391392 // and ensures Invariant-1.
392393 void editNewTag (const iterator& i)
393394 {
394- currentText () = currentText ().trimmed ();
395+ tags. editingIndex ()-> text = currentText ().trimmed ();
395396 auto inserted_at = tags.insert (i, Tag ());
396397 setEditingIndex (inserted_at);
397398 moveCursor (0 , false );
@@ -419,7 +420,7 @@ struct TagsEdit::Impl
419420 void removeSelection ()
420421 {
421422 cursor = select_start;
422- currentText () .remove (cursor, select_size);
423+ tags. editingIndex ()-> text .remove (cursor, select_size);
423424 deselectAll ();
424425 }
425426
@@ -428,7 +429,7 @@ struct TagsEdit::Impl
428429 if (hasSelection ()) {
429430 removeSelection ();
430431 } else {
431- currentText () .remove (--cursor, 1 );
432+ tags. editingIndex ()-> text .remove (--cursor, 1 );
432433 }
433434 }
434435
@@ -618,7 +619,6 @@ struct TagsEdit::Impl
618619 int select_start;
619620 int select_size;
620621 bool cross_deleter;
621- int hscroll{0 };
622622 QTextLayout text_layout;
623623
624624public:
@@ -641,11 +641,8 @@ struct TagsEdit::Impl
641641 const auto r = currentRect ();
642642 const auto txt_p = r.topLeft () + QPoint (TAG_INNER.left (), ((r.height () - fontHeight) / 2 ));
643643
644- // Nothing to draw. Don't draw anything to avoid adding text margins.
645- if (!it->isEmpty ()) {
646- // draw not terminated tag
647- text_layout.draw (&p, txt_p - scrollOffsets, formatting ());
648- }
644+ // draw not terminated tag
645+ text_layout.draw (&p, txt_p - scrollOffsets, formatting ());
649646
650647 // draw cursor
651648 if (drawCursor) {
@@ -663,19 +660,8 @@ TagsEdit::TagsEdit(QWidget* parent)
663660 , impl(new Impl(this ))
664661 , completer(new QCompleter)
665662{
666- QSizePolicy size_policy (QSizePolicy::Ignored, QSizePolicy::Preferred);
667- size_policy.setHeightForWidth (true );
668- setSizePolicy (size_policy);
669-
670- setFocusPolicy (Qt::StrongFocus);
671- viewport ()->setCursor (Qt::IBeamCursor);
672- setAttribute (Qt::WA_InputMethodEnabled, true );
673- setMouseTracking (true );
674-
663+ setReadOnly (false );
675664 setupCompleter ();
676- setCursorVisible (hasFocus ());
677- impl->updateDisplayText ();
678-
679665 viewport ()->setContentsMargins (TAG_H_SPACING, TAG_V_SPACING, TAG_H_SPACING, TAG_V_SPACING);
680666}
681667
@@ -694,6 +680,7 @@ void TagsEdit::setReadOnly(bool readOnly)
694680 setCursor (Qt::IBeamCursor);
695681 setAttribute (Qt::WA_InputMethodEnabled, true );
696682 }
683+ setMouseTracking (!m_readOnly);
697684 impl->setReadOnly (m_readOnly);
698685}
699686
@@ -837,24 +824,20 @@ void TagsEdit::mousePressEvent(QMouseEvent* event)
837824 }
838825}
839826
840- QSize TagsEdit::sizeHint () const
827+ QSize TagsEdit::viewportSizeHint () const
841828{
842- return minimumSizeHint ();
829+ return impl-> updateTagRenderStates ({ 0 , 0 , width (), 0 }). size ();
843830}
844831
845- QSize TagsEdit::minimumSizeHint () const
832+ bool TagsEdit::hasHeightForWidth () const
846833{
847- ensurePolished ();
848- QFontMetrics fm = fontMetrics ();
849- QRect rect (0 , 0 , fm.maxWidth () + TAG_CROSS_PADDING + TAG_CROSS_WIDTH, fm.height () + fm.leading ());
850- rect += TAG_INNER + contentsMargins () + viewport ()->contentsMargins () + viewportMargins ();
851- return rect.size ();
834+ return true ;
852835}
853836
854837int TagsEdit::heightForWidth (int w) const
855838{
856839 const auto content_width = w;
857- QRect contents_rect (0 , 0 , content_width, 100 );
840+ QRect contents_rect (0 , 0 , content_width, 32 );
858841 contents_rect -= contentsMargins () + viewport ()->contentsMargins () + viewportMargins ();
859842 contents_rect = impl->updateTagRenderStates (contents_rect);
860843 contents_rect += contentsMargins () + viewport ()->contentsMargins () + viewportMargins ();
0 commit comments