summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-06-02 15:38:17 (GMT)
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2010-01-14 12:47:23 (GMT)
commit9e4bf3772c808d8bf0016ae6674610bae2272613 (patch)
treec61ce8e18ea14139922438d7e2c63313a199898a
parentc46759628b95e388eb4c5bd8c87347a0f2ea0ad6 (diff)
downloadQt-9e4bf3772c808d8bf0016ae6674610bae2272613.zip
Qt-9e4bf3772c808d8bf0016ae6674610bae2272613.tar.gz
Qt-9e4bf3772c808d8bf0016ae6674610bae2272613.tar.bz2
Add operators and implicit sharing to QStaticText
Adds copy constructor, operator==, operator= and operator!= to QStaticText. Implemented using an implicitly shared private object.
-rw-r--r--src/gui/text/qstatictext.cpp105
-rw-r--r--src/gui/text/qstatictext.h10
-rw-r--r--src/gui/text/qstatictext_p.h2
3 files changed, 91 insertions, 26 deletions
diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp
index d665abe..b6c123b 100644
--- a/src/gui/text/qstatictext.cpp
+++ b/src/gui/text/qstatictext.cpp
@@ -112,14 +112,21 @@ QStaticText::QStaticText()
*/
QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &sz)
: d_ptr(new QStaticTextPrivate)
-{
- Q_D(QStaticText);
-
- d->textLayout->setText(text);
- d->textLayout->setFont(font);
- d->size = sz;
+{
+ d_ptr->textLayout->setText(text);
+ d_ptr->textLayout->setFont(font);
+ d_ptr->size = sz;
- d->init();
+ d_ptr->init();
+}
+
+/*!
+ Constructs a QStaticText object which is a copy of \a other.
+*/
+QStaticText::QStaticText(const QStaticText &other)
+{
+ d_ptr = other.d_ptr;
+ d_ptr->ref.ref();
}
/*!
@@ -127,8 +134,47 @@ QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &s
*/
QStaticText::~QStaticText()
{
- Q_D(QStaticText);
- delete d;
+ if (!d_ptr->ref.deref())
+ delete d_ptr;
+}
+
+/*!
+ \internal
+*/
+void QStaticText::detach()
+{
+ if (d_ptr->ref != 1)
+ qAtomicDetach(d_ptr);
+}
+
+/*!
+ Assigns \a other to this QStaticText.
+*/
+QStaticText &QStaticText::operator=(const QStaticText &other)
+{
+ qAtomicAssign(d_ptr, other.d_ptr);
+ return *this;
+}
+
+/*!
+ Compares \a other to this QStaticText. Returns true if the texts, fonts and maximum sizes
+ are equal.
+*/
+bool QStaticText::operator==(const QStaticText &other) const
+{
+ return (d_ptr == other.d_ptr
+ || (d_ptr->textLayout->text() == other.d_ptr->textLayout->text()
+ && d_ptr->textLayout->font() == other.d_ptr->textLayout->font()
+ && d_ptr->size == other.d_ptr->size));
+}
+
+/*!
+ Compares \a other to this QStaticText. Returns true if the texts, fonts or maximum sizes
+ are different.
+*/
+bool QStaticText::operator!=(const QStaticText &other) const
+{
+ return !(*this == other);
}
/*!
@@ -140,9 +186,10 @@ QStaticText::~QStaticText()
*/
void QStaticText::setText(const QString &text)
{
- Q_D(QStaticText);
- d->textLayout->setText(text);
- d->init();
+ detach();
+
+ d_ptr->textLayout->setText(text);
+ d_ptr->init();
}
/*!
@@ -152,8 +199,7 @@ void QStaticText::setText(const QString &text)
*/
QString QStaticText::text() const
{
- Q_D(const QStaticText);
- return d->textLayout->text();
+ return d_ptr->textLayout->text();
}
/*!
@@ -165,9 +211,10 @@ QString QStaticText::text() const
*/
void QStaticText::setFont(const QFont &font)
{
- Q_D(QStaticText);
- d->textLayout->setFont(font);
- d->init();
+ detach();
+
+ d_ptr->textLayout->setFont(font);
+ d_ptr->init();
}
/*!
@@ -177,8 +224,7 @@ void QStaticText::setFont(const QFont &font)
*/
QFont QStaticText::font() const
{
- Q_D(const QStaticText);
- return d->textLayout->font();
+ return d_ptr->textLayout->font();
}
/*!
@@ -191,9 +237,10 @@ QFont QStaticText::font() const
*/
void QStaticText::setMaximumSize(const QSizeF &maximumSize)
{
- Q_D(QStaticText);
- d->size = maximumSize;
- d->init();
+ detach();
+
+ d_ptr->size = maximumSize;
+ d_ptr->init();
}
/*!
@@ -203,8 +250,12 @@ void QStaticText::setMaximumSize(const QSizeF &maximumSize)
*/
QSizeF QStaticText::maximumSize() const
{
- Q_D(const QStaticText);
- return d->size;
+ return d_ptr->size;
+}
+
+QString QStaticText::toString() const
+{
+ return text();
}
QStaticTextPrivate::QStaticTextPrivate()
@@ -212,6 +263,12 @@ QStaticTextPrivate::QStaticTextPrivate()
{
}
+QStaticTextPrivate::QStaticTextPrivate(const QStaticTextPrivate &other)
+{
+ textLayout = new QTextLayout(other.textLayout->text(), other.textLayout->font());
+ size = other.size;
+}
+
QStaticTextPrivate::~QStaticTextPrivate()
{
delete textLayout;
diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h
index a25bef8..4976039 100644
--- a/src/gui/text/qstatictext.h
+++ b/src/gui/text/qstatictext.h
@@ -58,10 +58,9 @@ class Q_GUI_EXPORT QStaticText
public:
QStaticText();
QStaticText(const QString &text, const QFont &font = QFont(), const QSizeF &maximumSize = QSizeF());
+ QStaticText(const QStaticText &other);
~QStaticText();
- void isValid() const;
-
void setText(const QString &text);
QString text() const;
@@ -71,8 +70,15 @@ public:
void setMaximumSize(const QSizeF &maximumSize);
QSizeF maximumSize() const;
+ QStaticText &operator=(const QStaticText &);
+ bool operator==(const QStaticText &) const;
+ bool operator!=(const QStaticText &) const;
+
+ QString toString() const;
private:
+ void detach();
+
QStaticTextPrivate *d_ptr;
};
diff --git a/src/gui/text/qstatictext_p.h b/src/gui/text/qstatictext_p.h
index 1e334d7..773971a 100644
--- a/src/gui/text/qstatictext_p.h
+++ b/src/gui/text/qstatictext_p.h
@@ -62,12 +62,14 @@ class QStaticTextPrivate
{
public:
QStaticTextPrivate();
+ QStaticTextPrivate(const QStaticTextPrivate &other);
~QStaticTextPrivate();
void init();
QTextLayout *textLayout;
QSizeF size;
+ QAtomicInt ref;
static QStaticTextPrivate *get(const QStaticText *q);
};