1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/widget/emoticonswidget.cpp

196 lines
5.4 KiB
C++
Raw Normal View History

2014-08-01 20:32:51 +08:00
/*
Copyright © 2014-2015 by The qTox Project
2014-08-01 20:32:51 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
2014-08-01 20:32:51 +08:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
2014-08-01 20:32:51 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-08-01 20:32:51 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-08-01 20:32:51 +08:00
*/
#include "emoticonswidget.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/style.h"
#include "src/persistence/smileypack.h"
#include "src/persistence/settings.h"
2014-08-01 20:32:51 +08:00
#include <QPushButton>
#include <QRadioButton>
#include <QFile>
#include <QLayout>
#include <QGridLayout>
#include <QMouseEvent>
2014-08-01 20:32:51 +08:00
2014-10-19 01:30:55 +08:00
#include <math.h>
2014-08-01 20:32:51 +08:00
EmoticonsWidget::EmoticonsWidget(QWidget *parent) :
QMenu(parent)
{
setStyleSheet(Style::getStylesheet(":/ui/emoticonWidget/emoticonWidget.css"));
2014-08-01 20:32:51 +08:00
setLayout(&layout);
layout.addWidget(&stack);
QWidget* pageButtonsContainer = new QWidget;
QHBoxLayout* buttonLayout = new QHBoxLayout;
pageButtonsContainer->setLayout(buttonLayout);
layout.addWidget(pageButtonsContainer);
const int maxCols = 8;
const int maxRows = 8;
2014-08-01 20:32:51 +08:00
const int itemsPerPage = maxRows * maxCols;
const QList<QStringList>& emoticons = SmileyPack::getInstance().getEmoticons();
int itemCount = emoticons.size();
int pageCount = ceil(float(itemCount) / float(itemsPerPage));
2014-08-01 20:32:51 +08:00
int currPage = 0;
int currItem = 0;
int row = 0;
int col = 0;
2015-05-04 12:01:02 +08:00
// respect configured emoticon size
const int px = Settings::getInstance().getEmojiFontPointSize();
const QSize size(px, px);
2014-08-01 20:32:51 +08:00
// create pages
buttonLayout->addStretch();
for (int i = 0; i < pageCount; i++)
{
QGridLayout* pageLayout = new QGridLayout;
pageLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), maxRows, 0);
pageLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, maxCols);
QWidget* page = new QWidget;
page->setLayout(pageLayout);
stack.addWidget(page);
2014-08-10 21:11:59 +08:00
// page buttons are only needed if there is more than 1 page
if (pageCount > 1)
{
QRadioButton* pageButton = new QRadioButton;
pageButton->setProperty("pageIndex", i);
2014-09-25 21:38:47 +08:00
pageButton->setCursor(Qt::PointingHandCursor);
2014-08-10 21:11:59 +08:00
pageButton->setChecked(i == 0);
buttonLayout->addWidget(pageButton);
connect(pageButton, &QRadioButton::clicked, this, &EmoticonsWidget::onPageButtonClicked);
}
2014-08-01 20:32:51 +08:00
}
buttonLayout->addStretch();
for (const QStringList& set : emoticons)
{
QPushButton* button = new QPushButton;
2015-05-04 12:01:02 +08:00
button->setIcon(SmileyPack::getInstance().getAsIcon(set[0]).pixmap(size));
2014-08-01 20:32:51 +08:00
button->setToolTip(set.join(" "));
button->setProperty("sequence", set[0]);
2014-09-25 21:38:47 +08:00
button->setCursor(Qt::PointingHandCursor);
2014-08-01 20:32:51 +08:00
button->setFlat(true);
2015-05-04 12:01:02 +08:00
button->setIconSize(size);
button->setFixedSize(size);
2014-08-01 20:32:51 +08:00
connect(button, &QPushButton::clicked, this, &EmoticonsWidget::onSmileyClicked);
qobject_cast<QGridLayout*>(stack.widget(currPage)->layout())->addWidget(button, row, col);
col++;
currItem++;
// next row
if (col >= maxCols)
{
col = 0;
row++;
}
// next page
if (currItem >= itemsPerPage)
{
row = 0;
currItem = 0;
currPage++;
}
}
// calculates sizeHint
layout.activate();
}
void EmoticonsWidget::onSmileyClicked()
{
// hide the QMenu
hide();
2014-08-01 20:32:51 +08:00
// emit insert emoticon
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
if (sender)
2015-06-06 07:51:08 +08:00
{
QString sequence = sender->property("sequence").toString()
.replace("&lt;","<").replace("&gt;",">");
emit insertEmoticon(' ' + sequence + ' ');
}
2014-08-01 20:32:51 +08:00
}
void EmoticonsWidget::onPageButtonClicked()
{
QWidget* sender = qobject_cast<QRadioButton*>(QObject::sender());
if (sender)
{
int page = sender->property("pageIndex").toInt();
stack.setCurrentIndex(page);
}
}
QSize EmoticonsWidget::sizeHint() const
{
return layout.sizeHint();
}
void EmoticonsWidget::mouseReleaseEvent(QMouseEvent *ev)
{
if (!rect().contains(ev->pos()))
hide();
}
2014-11-08 21:07:51 +08:00
void EmoticonsWidget::mousePressEvent(QMouseEvent*)
{
}
2015-06-01 19:18:55 +08:00
void EmoticonsWidget::wheelEvent (QWheelEvent *e)
{
if (e->orientation() == Qt::Vertical)
{
if (e->delta() < 0 )
{
stack.setCurrentIndex(stack.currentIndex()+1);
}
else
{
stack.setCurrentIndex(stack.currentIndex()-1);
}
emit PageButtonsUpdate();
}
}
void EmoticonsWidget::PageButtonsUpdate()
{
QList<QRadioButton*> pageButtons = this->findChildren<QRadioButton*>(QString());
QRadioButton *t_pageButton;
foreach (t_pageButton,pageButtons)
{
if ( t_pageButton->property("pageIndex").toInt()==stack.currentIndex())
t_pageButton->setChecked(true);
else
t_pageButton->setChecked(false);
}
}