src/gui/widgets/qlabel.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    51 #include "qclipboard.h"
    51 #include "qclipboard.h"
    52 #include <qdebug.h>
    52 #include <qdebug.h>
    53 #include <qurl.h>
    53 #include <qurl.h>
    54 #include "qlabel_p.h"
    54 #include "qlabel_p.h"
    55 #include "private/qstylesheetstyle_p.h"
    55 #include "private/qstylesheetstyle_p.h"
       
    56 #include <qmath.h>
    56 
    57 
    57 QT_BEGIN_NAMESPACE
    58 QT_BEGIN_NAMESPACE
    58 
    59 
    59 /*!
    60 /*!
    60     \class QLabel
    61     \class QLabel
   659                     control->adjustSize();
   660                     control->adjustSize();
   660                 }
   661                 }
   661             } else {
   662             } else {
   662                 control->setTextWidth(-1);
   663                 control->setTextWidth(-1);
   663             }
   664             }
   664             br = QRect(QPoint(0, 0), control->size().toSize());
   665 
       
   666             QSizeF controlSize = control->size();
       
   667             br = QRect(QPoint(0, 0), QSize(qCeil(controlSize.width()), qCeil(controlSize.height())));
   665 
   668 
   666             // restore state
   669             // restore state
   667             control->setTextWidth(oldTextWidth);
   670             control->setTextWidth(oldTextWidth);
   668         } else {
   671         } else {
   669             // Turn off center alignment in order to avoid rounding errors for centering,
   672             // Turn off center alignment in order to avoid rounding errors for centering,
   779 {
   782 {
   780     Q_D(const QLabel);
   783     Q_D(const QLabel);
   781     return d->textInteractionFlags;
   784     return d->textInteractionFlags;
   782 }
   785 }
   783 
   786 
       
   787 /*!
       
   788     Selects text from position \a start and for \a length characters.
       
   789 
       
   790     \sa selectedText()
       
   791 
       
   792     \bold{Note:} The textInteractionFlags set on the label need to include
       
   793     either TextSelectableByMouse or TextSelectableByKeyboard.
       
   794 
       
   795     \since 4.7
       
   796 */
       
   797 void QLabel::setSelection(int start, int length)
       
   798 {
       
   799     Q_D(QLabel);
       
   800     if (d->control) {
       
   801         d->ensureTextPopulated();
       
   802         QTextCursor cursor = d->control->textCursor();
       
   803         cursor.setPosition(start);
       
   804         cursor.setPosition(start + length, QTextCursor::KeepAnchor);
       
   805         d->control->setTextCursor(cursor);
       
   806     }
       
   807 }
       
   808 
       
   809 /*!
       
   810     \property QLabel::hasSelectedText
       
   811     \brief whether there is any text selected
       
   812 
       
   813     hasSelectedText() returns true if some or all of the text has been
       
   814     selected by the user; otherwise returns false.
       
   815 
       
   816     By default, this property is false.
       
   817 
       
   818     \sa selectedText()
       
   819 
       
   820     \bold{Note:} The textInteractionFlags set on the label need to include
       
   821     either TextSelectableByMouse or TextSelectableByKeyboard.
       
   822 
       
   823     \since 4.7
       
   824 */
       
   825 bool QLabel::hasSelectedText() const
       
   826 {
       
   827     Q_D(const QLabel);
       
   828     if (d->control)
       
   829         return d->control->textCursor().hasSelection();
       
   830     return false;
       
   831 }
       
   832 
       
   833 /*!
       
   834     \property QLabel::selectedText
       
   835     \brief the selected text
       
   836 
       
   837     If there is no selected text this property's value is
       
   838     an empty string.
       
   839 
       
   840     By default, this property contains an empty string.
       
   841 
       
   842     \sa hasSelectedText()
       
   843 
       
   844     \bold{Note:} The textInteractionFlags set on the label need to include
       
   845     either TextSelectableByMouse or TextSelectableByKeyboard.
       
   846 
       
   847     \since 4.7
       
   848 */
       
   849 QString QLabel::selectedText() const
       
   850 {
       
   851     Q_D(const QLabel);
       
   852     if (d->control)
       
   853         return d->control->textCursor().selectedText();
       
   854     return QString();
       
   855 }
       
   856 
       
   857 /*!
       
   858     selectionStart() returns the index of the first selected character in the
       
   859     label or -1 if no text is selected.
       
   860 
       
   861     \sa selectedText()
       
   862 
       
   863     \bold{Note:} The textInteractionFlags set on the label need to include
       
   864     either TextSelectableByMouse or TextSelectableByKeyboard.
       
   865 
       
   866     \since 4.7
       
   867 */
       
   868 int QLabel::selectionStart() const
       
   869 {
       
   870     Q_D(const QLabel);
       
   871     if (d->control && d->control->textCursor().hasSelection())
       
   872         return d->control->textCursor().selectionStart();
       
   873     return -1;
       
   874 }
       
   875 
   784 /*!\reimp
   876 /*!\reimp
   785 */
   877 */
   786 QSize QLabel::sizeHint() const
   878 QSize QLabel::sizeHint() const
   787 {
   879 {
   788     Q_D(const QLabel);
   880     Q_D(const QLabel);
   860     if (!menu) {
   952     if (!menu) {
   861         ev->ignore();
   953         ev->ignore();
   862         return;
   954         return;
   863     }
   955     }
   864     ev->accept();
   956     ev->accept();
   865     menu->exec(ev->globalPos());
   957     menu->setAttribute(Qt::WA_DeleteOnClose);
   866     delete menu;
   958     menu->popup(ev->globalPos());
   867 #endif
   959 #endif
   868 }
   960 }
   869 
   961 
   870 /*!
   962 /*!
   871     \reimp
   963     \reimp