From 47dd119c76d6315aea511a1dbd90289a48c9dfbf Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Mon, 10 Sep 2012 09:37:26 +0200 Subject: [PATCH 1/3] Modified placeholder management in teacherguide --- src/gui/UBTeacherGuideWidgetsTools.cpp | 50 +++++++++++++------------- src/gui/UBTeacherGuideWidgetsTools.h | 7 +++- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/gui/UBTeacherGuideWidgetsTools.cpp b/src/gui/UBTeacherGuideWidgetsTools.cpp index 4456b243..b71bf82f 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.cpp +++ b/src/gui/UBTeacherGuideWidgetsTools.cpp @@ -148,30 +148,10 @@ void UBTGAdaptableText::setPlaceHolderText(QString text) setPlainText(mPlaceHolderText); } -void UBTGAdaptableText::keyPressEvent(QKeyEvent* e) -{ - if(isReadOnly()){ - // this is important if you set a placeholder. In this case even if the text field is readonly the - // keypressed event came here. So if you don't ignore it you'll have a flick on the text zone - e->ignore(); - return; - } - - if(toPlainText() == mPlaceHolderText){ - setPlainText(""); - } - setTextColor(QColor(Qt::black)); - QTextEdit::keyPressEvent(e); -} - void UBTGAdaptableText::keyReleaseEvent(QKeyEvent* e) { QTextEdit::keyReleaseEvent(e); - if(toPlainText().isEmpty()){ - setTextColor(QColor(Qt::lightGray)); - setPlainText(mPlaceHolderText); - } if(mMaximumLength && toPlainText().length()>mMaximumLength){ setPlainText(toPlainText().left(mMaximumLength)); QTextCursor tc(document()); @@ -183,8 +163,10 @@ void UBTGAdaptableText::keyReleaseEvent(QKeyEvent* e) void UBTGAdaptableText::showEvent(QShowEvent* e) { Q_UNUSED(e); - if(!mIsUpdatingSize && mHasPlaceHolder && toPlainText().isEmpty()) - setPlainText(mPlaceHolderText); + if(!mIsUpdatingSize && mHasPlaceHolder && toPlainText().isEmpty()){ + setTextColor(QColor(Qt::lightGray)); + setPlainText(mPlaceHolderText); + } else // If the teacherguide is collapsed, don't updated the size. Or set the size as the expanded size onTextChanged(); @@ -201,9 +183,7 @@ QString UBTGAdaptableText::text() void UBTGAdaptableText::onTextChanged() { - //qDebug() << ">> onTextChanged CALLED!"; qreal documentSize = document()->size().height(); - //qDebug() << ">> documentSize: " << documentSize << ", height: " << height(); if(height() == documentSize + mBottomMargin){ return; } @@ -248,6 +228,28 @@ void UBTGAdaptableText::bottomMargin(int newValue) onTextChanged(); } +void UBTGAdaptableText::focusInEvent(QFocusEvent* e){ + if(isReadOnly()){ + e->ignore(); + } + managePlaceholder(); + QTextEdit::focusInEvent(e); +} + +void UBTGAdaptableText::focusOutEvent(QFocusEvent* e){ + if(toPlainText().isEmpty()){ + setTextColor(QColor(Qt::lightGray)); + setPlainText(mPlaceHolderText); + } + QTextEdit::focusOutEvent(e); +} + +void UBTGAdaptableText::managePlaceholder(){ + if(toPlainText() == mPlaceHolderText){ + setTextColor(QColor(Qt::black)); + setPlainText(""); + } +} /*************************************************************************** * class UBTGDraggableWeb * diff --git a/src/gui/UBTeacherGuideWidgetsTools.h b/src/gui/UBTeacherGuideWidgetsTools.h index 2f87ccfd..364f3d99 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.h +++ b/src/gui/UBTeacherGuideWidgetsTools.h @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include "customWidgets/UBMediaWidget.h" @@ -103,11 +105,14 @@ public slots: void onTextChanged(); protected: - void keyPressEvent(QKeyEvent* e); void keyReleaseEvent(QKeyEvent* e); void showEvent(QShowEvent* e); + void focusInEvent(QFocusEvent* e); + void focusOutEvent(QFocusEvent* e); private: + void managePlaceholder(); + int mBottomMargin; QTreeWidgetItem* mpTreeWidgetItem; int mMinimumHeight; From 1d06110b5ef99dd7b136cc12d52ac3b6b22e2837 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Mon, 10 Sep 2012 12:07:33 +0200 Subject: [PATCH 2/3] Resolved the placeholder issue in the new ubtgadaptabletext implementation --- src/gui/UBTeacherGuideWidget.cpp | 4 ++++ src/gui/UBTeacherGuideWidgetsTools.cpp | 26 ++++++++++++++------------ src/gui/UBTeacherGuideWidgetsTools.h | 3 +-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/gui/UBTeacherGuideWidget.cpp b/src/gui/UBTeacherGuideWidget.cpp index 679da6a8..cfa76117 100644 --- a/src/gui/UBTeacherGuideWidget.cpp +++ b/src/gui/UBTeacherGuideWidget.cpp @@ -1023,14 +1023,18 @@ void UBTeacherGuidePageZeroWidget::switchToMode(tUBTGZeroPageMode mode) QString inputStyleSheet("QTextEdit { background: white; border-radius: 10px; border: 2px;}"); mpModePushButton->hide(); mpSessionTitle->setReadOnly(false); + mpSessionTitle->managePlaceholder(false); mpSessionTitle->setStyleSheet(inputStyleSheet); QFont titleFont(QApplication::font().family(), 11, -1); mpSessionTitle->document()->setDefaultFont(titleFont); mpAuthors->setReadOnly(false); + mpAuthors->managePlaceholder(false); mpAuthors->setStyleSheet(inputStyleSheet); mpObjectives->setReadOnly(false); + mpObjectives->managePlaceholder(false); mpObjectives->setStyleSheet(inputStyleSheet); mpKeywords->setReadOnly(false); + mpKeywords->managePlaceholder(false); mpKeywords->setStyleSheet(inputStyleSheet); mpSchoolLevelValueLabel->hide(); mpSchoolLevelBox->show(); diff --git a/src/gui/UBTeacherGuideWidgetsTools.cpp b/src/gui/UBTeacherGuideWidgetsTools.cpp index b71bf82f..7ecfbd27 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.cpp +++ b/src/gui/UBTeacherGuideWidgetsTools.cpp @@ -163,7 +163,7 @@ void UBTGAdaptableText::keyReleaseEvent(QKeyEvent* e) void UBTGAdaptableText::showEvent(QShowEvent* e) { Q_UNUSED(e); - if(!mIsUpdatingSize && mHasPlaceHolder && toPlainText().isEmpty()){ + if(!mIsUpdatingSize && mHasPlaceHolder && toPlainText().isEmpty() && !isReadOnly()){ setTextColor(QColor(Qt::lightGray)); setPlainText(mPlaceHolderText); } @@ -204,8 +204,6 @@ void UBTGAdaptableText::onTextChanged() setFocus(); } mIsUpdatingSize = false; - - } void UBTGAdaptableText::setInitialText(const QString& text) @@ -232,22 +230,26 @@ void UBTGAdaptableText::focusInEvent(QFocusEvent* e){ if(isReadOnly()){ e->ignore(); } - managePlaceholder(); + managePlaceholder(true); QTextEdit::focusInEvent(e); } void UBTGAdaptableText::focusOutEvent(QFocusEvent* e){ - if(toPlainText().isEmpty()){ - setTextColor(QColor(Qt::lightGray)); - setPlainText(mPlaceHolderText); - } + managePlaceholder(false); QTextEdit::focusOutEvent(e); } -void UBTGAdaptableText::managePlaceholder(){ - if(toPlainText() == mPlaceHolderText){ - setTextColor(QColor(Qt::black)); - setPlainText(""); +void UBTGAdaptableText::managePlaceholder(bool focus){ + if(focus){ + if(toPlainText() == mPlaceHolderText){ + setTextColor(QColor(Qt::black)); + setPlainText(""); + } + }else{ + if(toPlainText().isEmpty()){ + setTextColor(QColor(Qt::lightGray)); + setPlainText(mPlaceHolderText); + } } } diff --git a/src/gui/UBTeacherGuideWidgetsTools.h b/src/gui/UBTeacherGuideWidgetsTools.h index 364f3d99..933bdd30 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.h +++ b/src/gui/UBTeacherGuideWidgetsTools.h @@ -100,6 +100,7 @@ public: QString text(); void setInitialText(const QString& text); void setMaximumLength(int length); + void managePlaceholder(bool focus); public slots: void onTextChanged(); @@ -111,8 +112,6 @@ protected: void focusOutEvent(QFocusEvent* e); private: - void managePlaceholder(); - int mBottomMargin; QTreeWidgetItem* mpTreeWidgetItem; int mMinimumHeight; From 77aa2783a23e0db96d997db7b239616177be3e37 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Mon, 10 Sep 2012 15:39:57 +0200 Subject: [PATCH 3/3] Fixed some little things --- src/gui/UBTeacherGuideWidget.cpp | 2 +- src/gui/UBTeacherGuideWidgetsTools.cpp | 23 +++++++++++++++++++---- src/gui/UBTeacherGuideWidgetsTools.h | 1 + 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/gui/UBTeacherGuideWidget.cpp b/src/gui/UBTeacherGuideWidget.cpp index cfa76117..03e5190d 100644 --- a/src/gui/UBTeacherGuideWidget.cpp +++ b/src/gui/UBTeacherGuideWidget.cpp @@ -1023,7 +1023,7 @@ void UBTeacherGuidePageZeroWidget::switchToMode(tUBTGZeroPageMode mode) QString inputStyleSheet("QTextEdit { background: white; border-radius: 10px; border: 2px;}"); mpModePushButton->hide(); mpSessionTitle->setReadOnly(false); - mpSessionTitle->managePlaceholder(false); + mpSessionTitle->managePlaceholder(true); mpSessionTitle->setStyleSheet(inputStyleSheet); QFont titleFont(QApplication::font().family(), 11, -1); mpSessionTitle->document()->setDefaultFont(titleFont); diff --git a/src/gui/UBTeacherGuideWidgetsTools.cpp b/src/gui/UBTeacherGuideWidgetsTools.cpp index 7ecfbd27..c67b09a8 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.cpp +++ b/src/gui/UBTeacherGuideWidgetsTools.cpp @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include "UBTeacherGuideWidgetsTools.h" @@ -190,10 +193,11 @@ void UBTGAdaptableText::onTextChanged() mIsUpdatingSize = true; - if(documentSize < mMinimumHeight) - setFixedHeight(mMinimumHeight); - else - setFixedHeight(documentSize+mBottomMargin); + if(documentSize < mMinimumHeight){ + setFixedHeight(mMinimumHeight); + }else{ + setFixedHeight(documentSize+mBottomMargin); + } updateGeometry(); //to trig a resize on the tree widget item @@ -245,6 +249,7 @@ void UBTGAdaptableText::managePlaceholder(bool focus){ setTextColor(QColor(Qt::black)); setPlainText(""); } + setCursorToTheEnd(); }else{ if(toPlainText().isEmpty()){ setTextColor(QColor(Qt::lightGray)); @@ -253,6 +258,16 @@ void UBTGAdaptableText::managePlaceholder(bool focus){ } } +void UBTGAdaptableText::setCursorToTheEnd(){ + QTextDocument* doc = document(); + if(NULL != doc){ + QTextBlock block = doc->lastBlock(); + QTextCursor cursor(doc); + cursor.setPosition(block.position() + block.length() - 1); + setTextCursor(cursor); + } +} + /*************************************************************************** * class UBTGDraggableWeb * ***************************************************************************/ diff --git a/src/gui/UBTeacherGuideWidgetsTools.h b/src/gui/UBTeacherGuideWidgetsTools.h index 933bdd30..cd37fe2d 100644 --- a/src/gui/UBTeacherGuideWidgetsTools.h +++ b/src/gui/UBTeacherGuideWidgetsTools.h @@ -112,6 +112,7 @@ protected: void focusOutEvent(QFocusEvent* e); private: + void setCursorToTheEnd(); int mBottomMargin; QTreeWidgetItem* mpTreeWidgetItem; int mMinimumHeight;