homeview.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "homeview.h"
  2. #include <QtCore>
  3. #include <QtGui>
  4. class LineEdit: public QLineEdit
  5. {
  6. public:
  7. LineEdit(QWidget *parent = 0): QLineEdit(parent) {}
  8. void paintEvent(QPaintEvent *event) {
  9. QLineEdit::paintEvent(event);
  10. if (text().isEmpty()) {
  11. QPainter p(this);
  12. int flags = Qt::AlignLeft | Qt::AlignVCenter;
  13. p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
  14. p.drawText(rect().adjusted(10, 0, 0, 0), flags, "Enter your nick");
  15. p.end();
  16. }
  17. }
  18. };
  19. HomeView::HomeView(QWidget *parent)
  20. : QWidget(parent)
  21. {
  22. vLayout = new QVBoxLayout(parent);
  23. //vLayout->setGeometry(parent->geometry());
  24. textNick = new LineEdit(parent);
  25. textNick->setMinimumHeight(50);
  26. buttonLogin = new QPushButton(parent);
  27. buttonLogin->setText("Login");
  28. buttonLogin->setFixedHeight(60);
  29. buttonExit = new QPushButton(parent);
  30. buttonExit->setText("Exit");
  31. buttonExit->setFixedHeight(60);
  32. connect(buttonLogin, SIGNAL(clicked()), SLOT(tryLogin()));
  33. connect(buttonExit, SIGNAL(clicked()), SLOT(exitApp()));
  34. connect(textNick, SIGNAL(returnPressed()), this, SLOT(tryLogin()));
  35. picTitle = new QWidget(parent);
  36. picTitle->resize(300, 400);
  37. picTitle->setAutoFillBackground(true);
  38. labelStatus = new QLabel(parent);
  39. labelStatus->setAlignment(Qt::AlignCenter);
  40. labelStatus->setMinimumHeight(50);
  41. labelStatus->setFrameStyle(2);
  42. QHBoxLayout *layoutButtons = new QHBoxLayout(this);
  43. layoutButtons->addWidget(buttonExit);
  44. layoutButtons->addWidget(buttonLogin);
  45. //vLayout->addWidget(buttonExit);
  46. vLayout->addWidget(picTitle, 100);
  47. vLayout->addWidget(labelStatus);
  48. vLayout->addWidget(textNick);
  49. //vLayout->addWidget(buttonLogin);
  50. vLayout->addLayout(layoutButtons);
  51. setLayout(vLayout);
  52. }
  53. void HomeView::exitApp()
  54. {
  55. QApplication::quit();
  56. }
  57. void HomeView::init(NetworkApi *netApi)
  58. {
  59. networkApi = netApi;
  60. connect(networkApi, SIGNAL(loginComplete()), this, SLOT(processLogin(QString*)));
  61. connect(networkApi, SIGNAL(commError(QString*)), this, SLOT(processError(QString*)));
  62. connect(networkApi, SIGNAL(usersReceived(QString*)), this, SLOT(processLogin(QString*)));
  63. }
  64. // login into chat service
  65. void HomeView::tryLogin()
  66. {
  67. labelStatus->setText("Connecting...");
  68. // process login
  69. if(textNick->text().count() <= 0)
  70. {
  71. labelStatus->setText("Please enter nick.");
  72. textNick->setFocus();
  73. return;
  74. }
  75. labelStatus->setText(textNick->text().append(" connecting..."));
  76. networkApi->loginUser(&textNick->text());
  77. }
  78. void HomeView::processLogin(QString *xmlData)
  79. {
  80. if(!this->isHidden())
  81. {
  82. qDebug(QString("processLogin: ").append(xmlData).toAscii());
  83. // issue login complete signal
  84. emit loginComplete(textNick->text());
  85. }
  86. }
  87. void HomeView::processError(QString *errorData)
  88. {
  89. if(!this->isHidden())
  90. {
  91. labelStatus->setText(*errorData);
  92. }
  93. }
  94. void HomeView::resetUI()
  95. {
  96. labelStatus->clear();
  97. textNick->clear();
  98. textNick->setFocus();
  99. }
  100. void HomeView::focusInEvent(QFocusEvent *event)
  101. {
  102. textNick->setFocus();
  103. QWidget::focusInEvent(event);
  104. }