Advertisement
Guest User

chat widget

a guest
Feb 15th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1.  
  2. //mylabelchat.h
  3.  
  4. #ifndef MYLABELCHAT_H
  5. #define MYLABELCHAT_H
  6.  
  7. #include <QWidget>
  8. #include <QLabel>
  9. #include <QObject>
  10. #include <QMouseEvent>
  11. #include <QPaintEvent>
  12. #include <QEvent>
  13.  
  14. class QGraphicsView;
  15. class QGraphicsPixmapItem;
  16. class QGraphicsScene;
  17. class QPushButton;
  18.  
  19. class MyLabelChat : public QWidget
  20. {
  21.     Q_OBJECT
  22. public:
  23.     MyLabelChat(QPixmap pixmap, QWidget *parent = nullptr);
  24.  
  25.     QPushButton *button_close;
  26.     bool _flag_active = false;
  27.     int _status = 0;
  28.     int _num_row = 0;
  29.     int _num_mess = 0;
  30.     QLabel *_text;
  31.  
  32.  
  33.     void my_repaint(QColor col, int status);
  34.     void my_update();
  35.     void set_text(QString);
  36.  
  37.     signals:
  38.         void signal_chatuser_pressed(const QString &);
  39.         void signal_delete_user(const QString &);
  40.  
  41.     protected:
  42.         bool event( QEvent *event) override;
  43.         void mousePressEvent(QMouseEvent *event) override;
  44.  
  45.     private slots:
  46.         void delete_chat_user();
  47.  
  48.     private:
  49.  
  50.     QGraphicsView *foto;
  51.     QGraphicsScene *scene;
  52.     QColor color;
  53.     QPixmap _pixmap;
  54. };
  55.  
  56. #endif // MYLABELCHAT_H
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //mylabelchat.cpp
  65.  
  66.  
  67. #include "mylabelchat.h"
  68.  
  69. #include <QHBoxLayout>
  70. #include <QPainter>
  71. #include <QDebug>
  72. #include <QGraphicsView>
  73. #include <QGraphicsPixmapItem>
  74. #include <QGraphicsScene>
  75. #include <QPushButton>
  76.  
  77. #include "global.h"
  78.  
  79. MyLabelChat::MyLabelChat(QPixmap pixmap, QWidget *parent) : QWidget(parent)
  80. {
  81.     _pixmap = pixmap.scaled(QSize(80, 80), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  82.  
  83.     QHBoxLayout *box = new QHBoxLayout(this);
  84.     box->setSpacing(0);
  85.     box->setMargin(2);
  86.     scene = new QGraphicsScene(this);
  87.  
  88.     foto = new QGraphicsView(scene);
  89.     foto->setSceneRect(QRect(0, 0, 80, 80));
  90.     foto->setScene(scene);
  91.     foto->setRenderHint(QPainter::Antialiasing, true);
  92.     foto->setStyleSheet("border:none;");
  93.     foto->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  94.     foto->setFixedSize(80, 80);
  95.  
  96.     _text = new QLabel(this);
  97.     _text->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
  98.     _text->setFixedHeight(80);
  99.  
  100.     button_close = new QPushButton(_text);
  101.     button_close->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  102.     button_close->setFixedSize(16, 16);
  103.     button_close->move(_text->width() - 12, 0);
  104.     button_close->setStyleSheet("QPushButton{ border-image: url(:/image/delete2.png); }");
  105.     button_close->setToolTip("Удалить");
  106.     button_close->setVisible(false);
  107.     connect(button_close, &QPushButton::clicked, this, &MyLabelChat::delete_chat_user);
  108.  
  109.     color = this->palette().color(QPalette::Background);
  110.  
  111.     this->setFixedHeight(80);
  112.  
  113.     box->addWidget(foto);
  114.     box->addWidget(_text);
  115.     my_update();
  116. }
  117.  
  118. void MyLabelChat::set_text(QString str)
  119. {
  120. _text->setText(str);
  121. }
  122.  
  123. bool MyLabelChat::event(QEvent *event)
  124. {
  125.     if (event->type() == QEvent::Enter )
  126.     {
  127.         if(!_flag_active)
  128.         my_repaint(QColor(186,226,255), _status);
  129.  
  130.         button_close->setVisible(true);
  131.     }
  132.     else
  133.     if(event->type() == QEvent::Leave)
  134.     {
  135.         if(!_flag_active)
  136.         my_repaint(color, _status);
  137.  
  138.         button_close->setVisible(false);
  139.     }
  140.     return QWidget::event(event);
  141. }
  142.  
  143. void MyLabelChat::mousePressEvent(QMouseEvent *event)
  144. {
  145.     if(event->button() == Qt::LeftButton)
  146.     {
  147.     my_repaint(QColor(166,206,255), _status);
  148.  
  149.     _flag_active = true;
  150.     emit signal_chatuser_pressed(this->objectName());
  151.     }
  152. }
  153.  
  154. void MyLabelChat::delete_chat_user()
  155. {
  156.     emit signal_delete_user(this->objectName());
  157. }
  158.  
  159.  
  160. void MyLabelChat::my_repaint(QColor col, int status)
  161. {
  162.     scene->clear();
  163.     QColor ell_color;
  164.  
  165.     switch (status)
  166.     {
  167.     case Enabled:
  168.     ell_color = Qt::green;
  169.     break;
  170.     case Lunch:
  171.     ell_color = Qt::blue;
  172.     break;
  173.     case Conference:
  174.     ell_color = Qt::red;
  175.     break;
  176.     }
  177.  
  178.     QPixmap temp = _pixmap;
  179.     int pen_widht = 40;
  180.     int border = 8;
  181.  
  182.     scene->addPixmap(temp);
  183.  
  184.     QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem;
  185.     ellipse->setRect(QRectF(-pen_widht/2 + border/2, -pen_widht/2 + border/2,
  186.             temp.width()+pen_widht -border, temp.height()+pen_widht -border));
  187.     ellipse->setBrush(QBrush(Qt::transparent));
  188.     ellipse->setPen(QPen(col,pen_widht));
  189.  
  190.     scene->addItem(ellipse);//круглая рамка
  191.  
  192.     if(status > 0)
  193.     {
  194.     QGraphicsEllipseItem *ellipse2 = new QGraphicsEllipseItem;
  195.  
  196.     ellipse2->setRect(QRectF(56, 60, 16, 16));
  197.     ellipse2->setBrush(ell_color);
  198.     ellipse2->setPen(QPen(Qt::white,2));
  199.     scene->addItem(ellipse2);//статус
  200.     }
  201.  
  202.     if(_num_mess)
  203.     {
  204.     int size = 28;
  205.     QPixmap pix(":/image/email_3.png");
  206.     QPixmap new_pix = pix.scaledToWidth(size, Qt::SmoothTransformation);
  207.  
  208.     QPainter painter;//количество непрочитанных сообщений
  209.     painter.begin(&new_pix);
  210.     painter.setRenderHint(QPainter::Antialiasing);
  211.     painter.setPen(Qt::red);
  212.     painter.setFont(QFont("Arial", 15, QFont::Bold));
  213.     painter.drawText(QRectF(0, -3, size, size), Qt::AlignCenter, QString::number(_num_mess));
  214.     painter.end();
  215.  
  216.     QGraphicsPixmapItem *item = scene->addPixmap(new_pix);
  217.     item->setPos(QPointF(50, 6));
  218.  
  219.     }
  220.     _text->setStyleSheet(QString("background-color: "+ col.name()));
  221. }
  222.  
  223. void MyLabelChat::my_update()
  224. {
  225.     if(!_flag_active)
  226.  my_repaint(color, _status);
  227.     else
  228.  my_repaint(QColor(166,206,255), _status);
  229.  
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. //использование
  238. QPixmap pixmap("image.png");
  239. MyLabelChat *label = new MyLabelChat(pixmap, this);
  240.  
  241. QVBoxLayout* layout = new QVBoxLayout(this);
  242. layout->addWidget(label);
  243.  
  244. connect(label, &MyLabelChat::signal_chatuser_pressed, this, &MainWindow::slot_chatuser_pressed);
  245. connect(label, &MyLabelChat::signal_delete_user, this, &MainWindow::slot_delete_user);
  246.  
  247. label->set_text("name");
  248. label->show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement