Added character counter in update location dialog
authorKatri Kaikkonen <katri.kaikkonen@ixonos.com>
Thu, 3 Jun 2010 07:38:38 +0000 (10:38 +0300)
committerKatri Kaikkonen <katri.kaikkonen@ixonos.com>
Thu, 3 Jun 2010 07:38:38 +0000 (10:38 +0300)
src/ui/updatelocation/updatelocationdialog.cpp
src/ui/updatelocation/updatelocationdialog.h

index a578bb5..7483668 100755 (executable)
 #include <QDebug>
 #include "updatelocationdialog.h"
 
+const int MAX_CHAR = 255; ///< Maximum input length for QTextEdit
+const QFont NOKIA_FONT_VERY_SMALL = QFont("Nokia Sans", 10, QFont::Normal); ///< Very small font
+
 UpdateLocationDialog::UpdateLocationDialog(QWidget *parent)
     : QDialog(parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     setWindowTitle(tr("Update Location"));
 
-    QScrollArea *scrollArea = new QScrollArea(this);
-    QGridLayout *gridLayout = new QGridLayout(this);
+    QScrollArea *scrollArea = new QScrollArea();
+    QGridLayout *gridLayout = new QGridLayout();
     QGroupBox *groupBox = new QGroupBox(scrollArea);
 
     m_textEdit = new QTextEdit;
     m_locationLabel = new QLabel;
     m_locationLabel->setWordWrap(true);
+    m_characterLabel = new QLabel;
+    m_characterLabel->setNum(MAX_CHAR);
+    m_characterLabel->setFont(NOKIA_FONT_VERY_SMALL);
+    m_characterNumberLabel = new QLabel(tr("Characters left:"));
+    m_characterNumberLabel->setFont(NOKIA_FONT_VERY_SMALL);
     m_checkBox = new QCheckBox(tr("Publish on Facebook"));
 
     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
@@ -43,22 +52,29 @@ UpdateLocationDialog::UpdateLocationDialog(QWidget *parent)
     QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
     sendButton->setText(tr("Send"));
 
-    QFormLayout *form = new QFormLayout();
-    form->addRow(new QLabel(tr("Location:")), m_locationLabel);
-    form->addRow(new QLabel(tr("Message:")), m_textEdit);
-    form->addWidget(m_checkBox);
+    QGridLayout *gridLayout2 = new QGridLayout();
+    gridLayout2->setMargin(0);
+    gridLayout2->setSpacing(0);
+    gridLayout2->addWidget(new QLabel(tr("Location:")),0, 0, 1, 1, Qt::AlignTop);
+    gridLayout2->addWidget(m_locationLabel, 0, 1, 1, 3);
+    gridLayout2->addWidget(new QLabel(tr("Message:")),1, 0, 1, 1, Qt::AlignTop);
+    gridLayout2->addWidget(m_textEdit, 1, 1, 1, 3);
+    gridLayout2->addWidget(m_characterNumberLabel, 4, 1, 1, 1, Qt::AlignLeft);
+    gridLayout2->addWidget(m_characterLabel, 4, 1, 1, 1, Qt::AlignRight);
+    gridLayout2->addWidget(m_checkBox, 5, 1, 1, 3);
 
     new TextEditAutoResizer(m_textEdit);
 
-    groupBox->setLayout(form);
+    groupBox->setLayout(gridLayout2);
     scrollArea->setWidget(groupBox);
     scrollArea->setWidgetResizable(true);
     gridLayout->addWidget(scrollArea, 0, 0, 2, 1);
-    gridLayout->addWidget(buttonBox, 0, 1, 1, 1);
+    gridLayout->addWidget(buttonBox, 1, 1, 1, 1);
     setLayout(gridLayout);
 
     connect(sendButton, SIGNAL(clicked()), this, SLOT(sendUpdate()));
     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
+    connect(m_textEdit, SIGNAL(textChanged()), this, SLOT(characterCounter()));
 
     scrollArea->show();
 }
@@ -71,6 +87,7 @@ UpdateLocationDialog::~UpdateLocationDialog()
 void UpdateLocationDialog::setAddress(const QString &address)
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     m_locationLabel->setText(address);
 }
 
@@ -83,3 +100,20 @@ void UpdateLocationDialog::sendUpdate()
 
     this->close();
 }
+
+void UpdateLocationDialog::characterCounter()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QString message;
+    int charLeft = 0;
+
+    charLeft = MAX_CHAR - m_textEdit->toPlainText().toUtf8().size();
+    m_characterLabel->setNum(charLeft);
+
+    if(charLeft < 0){
+            message = (m_textEdit -> toPlainText()).left(MAX_CHAR); //more than maxChar so keep only the first maxChar characters
+            m_textEdit -> setText(message); //set these characters as the visble
+            m_textEdit -> moveCursor(QTextCursor::End,QTextCursor::MoveAnchor); //set cursor to the end
+    }
+}
index 1464886..56639fa 100755 (executable)
@@ -61,9 +61,7 @@ public:
 /*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
-
 public slots:
-
     /**
     * @brief Public slot, which is used to set the street address to location label
     *
@@ -72,19 +70,22 @@ public slots:
     void setAddress(const QString &address);
 
 private slots:
-
     /**
     * @brief Private slot, which is used to connect send button
     *
     */
     void sendUpdate();
 
+    /**
+    * @brief Private slot, which counts message text length
+    *
+    */
+    void characterCounter();
+
 /*******************************************************************************
  * SIGNALS
  ******************************************************************************/
-
 signals:
-
     /**
     * @brief Signal Signal for requestLocationUpdate from SituareEngine via MainWindow class
     *
@@ -96,10 +97,10 @@ signals:
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
-
 private:
-
     QCheckBox *m_checkBox; ///< Pointer to CheckBox
+    QLabel *m_characterLabel; ///< Pointer to characterLabel
+    QLabel *m_characterNumberLabel;
     QLabel *m_locationLabel; ///< Pointer to locationLabel
     QTextEdit *m_textEdit; ///< Pointer to TextEdit
 };