reworked the QTextEdit placeholder

preferencesAboutTextFull
Claudio Valerio 13 years ago
parent 35467c4eaf
commit e36c0c8e48
  1. 42
      src/gui/UBTeacherGuideWidgetsTools.cpp
  2. 7
      src/gui/UBTeacherGuideWidgetsTools.h

@ -71,6 +71,7 @@ UBTGActionWidget::UBTGActionWidget(QTreeWidgetItem* widget, QWidget* parent, con
mpOwner->insertItems(0,qslOwner); mpOwner->insertItems(0,qslOwner);
mpOwner->setCurrentIndex(0); mpOwner->setCurrentIndex(0);
mpTask = new UBTGAdaptableText(widget,this); mpTask = new UBTGAdaptableText(widget,this);
mpTask->setPlaceHolderText(tr("Type task here ..."));
mpTask->setAcceptRichText(true); mpTask->setAcceptRichText(true);
mpTask->setTextColor(QColor().green()); mpTask->setTextColor(QColor().green());
mpTask->setObjectName("ActionWidgetTaskTextEdit"); mpTask->setObjectName("ActionWidgetTaskTextEdit");
@ -102,6 +103,7 @@ UBTGAdaptableText::UBTGAdaptableText(QTreeWidgetItem* widget, QWidget* parent, c
, mpTreeWidgetItem(widget) , mpTreeWidgetItem(widget)
, mMinimumHeight(20) , mMinimumHeight(20)
, mHasPlaceHolder(false) , mHasPlaceHolder(false)
, mIsUpdatingSize(false)
{ {
setObjectName(name); setObjectName(name);
setStyleSheet( "QWidget {background: white; border:1 solid #999999; border-radius : 10px; padding: 2px;}"); setStyleSheet( "QWidget {background: white; border:1 solid #999999; border-radius : 10px; padding: 2px;}");
@ -113,36 +115,39 @@ UBTGAdaptableText::UBTGAdaptableText(QTreeWidgetItem* widget, QWidget* parent, c
void UBTGAdaptableText::setPlaceHolderText(QString text) void UBTGAdaptableText::setPlaceHolderText(QString text)
{ {
mHasPlaceHolder = true;
// the space addition is to make this string unique and check against it to know // the space addition is to make this string unique and check against it to know
// if we are talking about a typed string or the placeholder string // if we are talking about a typed string or the placeholder string
mPlaceHolderText = text + " "; mPlaceHolderText = text + " ";
setTextColor(QColor(Qt::lightGray)); setTextColor(QColor(Qt::lightGray));
setText(mPlaceHolderText); setPlainText(mPlaceHolderText);
onTextChanged();
if(isHidden())
show();
mHasPlaceHolder = true;
} }
void UBTGAdaptableText::focusInEvent(QFocusEvent *e) void UBTGAdaptableText::keyPressEvent(QKeyEvent* e)
{ {
if(mHasPlaceHolder && toPlainText() == mPlaceHolderText){ if(toPlainText() == mPlaceHolderText){
setText("");
setTextColor(QColor(Qt::black)); setTextColor(QColor(Qt::black));
setPlainText("");
} }
QTextEdit::keyPressEvent(e);
e->accept();
} }
void UBTGAdaptableText::keyReleaseEvent(QKeyEvent* e)
void UBTGAdaptableText::focusOutEvent(QFocusEvent *e)
{ {
if(mHasPlaceHolder && toPlainText().length() == 0){ QTextEdit::keyReleaseEvent(e);
if(toPlainText().isEmpty()){
setTextColor(QColor(Qt::lightGray)); setTextColor(QColor(Qt::lightGray));
setText(mPlaceHolderText); setPlainText(mPlaceHolderText);
} }
e->accept(); }
void UBTGAdaptableText::showEvent(QShowEvent* e)
{
Q_UNUSED(e);
if(!mIsUpdatingSize && mHasPlaceHolder && toPlainText().isEmpty())
setPlainText(mPlaceHolderText);
} }
QString UBTGAdaptableText::text() QString UBTGAdaptableText::text()
@ -156,6 +161,7 @@ QString UBTGAdaptableText::text()
void UBTGAdaptableText::onTextChanged() void UBTGAdaptableText::onTextChanged()
{ {
mIsUpdatingSize = true;
if(document()->size().height() < mMinimumHeight) if(document()->size().height() < mMinimumHeight)
setFixedHeight(mMinimumHeight); setFixedHeight(mMinimumHeight);
else else
@ -167,6 +173,7 @@ void UBTGAdaptableText::onTextChanged()
mpTreeWidgetItem->setExpanded(true); mpTreeWidgetItem->setExpanded(true);
setFocus(); setFocus();
} }
mIsUpdatingSize = false;
} }
void UBTGAdaptableText::showText(const QString & text) void UBTGAdaptableText::showText(const QString & text)
@ -210,7 +217,7 @@ UBTGMediaWidget::UBTGMediaWidget(QTreeWidgetItem* widget, QWidget* parent,const
setAcceptDrops(true); setAcceptDrops(true);
addWidget(mpDropMeWidget); addWidget(mpDropMeWidget);
setMinimumHeight(100); setMinimumHeight(200);
} }
UBTGMediaWidget::UBTGMediaWidget(QString relativePath, QTreeWidgetItem* widget, QWidget* parent,const char* name): QStackedWidget(parent) UBTGMediaWidget::UBTGMediaWidget(QString relativePath, QTreeWidgetItem* widget, QWidget* parent,const char* name): QStackedWidget(parent)
@ -307,6 +314,7 @@ void UBTGMediaWidget::createWorkWidget(QString& path)
mpLayout = new QVBoxLayout(mpWorkWidget); mpLayout = new QVBoxLayout(mpWorkWidget);
if(!mIsPresentationMode){ if(!mIsPresentationMode){
mpTitle = new UBTGAdaptableText(mpTreeWidgetItem,mpWorkWidget); mpTitle = new UBTGAdaptableText(mpTreeWidgetItem,mpWorkWidget);
mpTitle->setPlaceHolderText(tr("Type title here..."));
mpLayout->addWidget(mpTitle,1); mpLayout->addWidget(mpTitle,1);
} }
if(mpMediaLabelWidget){ if(mpMediaLabelWidget){
@ -347,8 +355,6 @@ void UBTGMediaWidget::parseMimeData(const QMimeData* pMimeData)
qDebug() << "No mime data present"; qDebug() << "No mime data present";
createWorkWidget(path); createWorkWidget(path);
QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(path);
qDebug() << mimeType;
} }
void UBTGMediaWidget::dropEvent(QDropEvent* event) void UBTGMediaWidget::dropEvent(QDropEvent* event)

@ -96,14 +96,17 @@ public slots:
void onTextChanged(); void onTextChanged();
protected: protected:
void focusInEvent(QFocusEvent *e); void keyPressEvent(QKeyEvent* e);
void focusOutEvent(QFocusEvent *e); void keyReleaseEvent(QKeyEvent* e);
void showEvent(QShowEvent* e);
private: private:
int mBottomMargin; int mBottomMargin;
QTreeWidgetItem* mpTreeWidgetItem; QTreeWidgetItem* mpTreeWidgetItem;
int mMinimumHeight; int mMinimumHeight;
bool mHasPlaceHolder; bool mHasPlaceHolder;
QString mPlaceHolderText; QString mPlaceHolderText;
bool mIsUpdatingSize;
}; };

Loading…
Cancel
Save