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

Moved the context menu to GenericChatForm

This commit is contained in:
krepa098 2015-01-04 14:21:05 +01:00
parent f4c6bc3452
commit 64024c77a6
4 changed files with 54 additions and 50 deletions

View File

@ -21,10 +21,8 @@
#include <QDebug> #include <QDebug>
#include <QScrollBar> #include <QScrollBar>
#include <QApplication> #include <QApplication>
#include <QMenu>
#include <QClipboard> #include <QClipboard>
#include <QFile> #include <QAction>
#include <QFileDialog>
template<class T> template<class T>
T clamp(T x, T min, T max) T clamp(T x, T min, T max)
@ -50,6 +48,7 @@ ChatLog::ChatLog(QWidget* parent)
setViewportUpdateMode(BoundingRectViewportUpdate); setViewportUpdateMode(BoundingRectViewportUpdate);
//setRenderHint(QPainter::TextAntialiasing); //setRenderHint(QPainter::TextAntialiasing);
setAcceptDrops(false); setAcceptDrops(false);
setContextMenuPolicy(Qt::CustomContextMenu);
const QColor selGraphColor = QColor(166,225,255); const QColor selGraphColor = QColor(166,225,255);
selGraphItem = scene->addRect(0,0,0,0,selGraphColor.darker(120),selGraphColor); selGraphItem = scene->addRect(0,0,0,0,selGraphColor.darker(120),selGraphColor);
@ -228,14 +227,22 @@ void ChatLog::mousePressEvent(QMouseEvent* ev)
{ {
if(!isOverSelection(scenePos)) if(!isOverSelection(scenePos))
clearSelection(); clearSelection();
showContextMenu(ev->globalPos(), scenePos);
} }
} }
void ChatLog::mouseReleaseEvent(QMouseEvent* ev) void ChatLog::mouseReleaseEvent(QMouseEvent* ev)
{ {
QGraphicsView::mouseReleaseEvent(ev); QGraphicsView::mouseReleaseEvent(ev);
QPointF scenePos = mapToScene(ev->pos());
if(ev->button() == Qt::RightButton)
{
if(!isOverSelection(scenePos))
clearSelection();
emit customContextMenuRequested(ev->pos());
}
} }
void ChatLog::mouseMoveEvent(QMouseEvent* ev) void ChatLog::mouseMoveEvent(QMouseEvent* ev)
@ -461,41 +468,9 @@ bool ChatLog::isEmpty() const
return lines.isEmpty(); return lines.isEmpty();
} }
void ChatLog::showContextMenu(const QPoint& globalPos, const QPointF& scenePos) bool ChatLog::hasTextToBeCopied() const
{ {
QMenu menu; return selectionMode != None;
// populate
QAction* copyAction = menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy"));
menu.addSeparator();
QAction* clearAction = menu.addAction(QIcon::fromTheme("edit-clear") ,tr("Clear chat log"));
QAction* saveAction = menu.addAction(QIcon::fromTheme("document-save") ,tr("Save chat log"));
if(!isOverSelection(scenePos))
copyAction->setDisabled(true);
// show
QAction* action = menu.exec(globalPos);
if(action == copyAction)
copySelectedText();
if(action == clearAction)
clear();
if(action == saveAction)
{
QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
file.write(toPlainText().toUtf8());
file.close();
}
} }
void ChatLog::clear() void ChatLog::clear()

View File

@ -53,6 +53,7 @@ public:
QString toPlainText() const; QString toPlainText() const;
bool isEmpty() const; bool isEmpty() const;
bool hasTextToBeCopied() const;
protected: protected:
QRect getVisibleRect() const; QRect getVisibleRect() const;
@ -71,7 +72,6 @@ protected:
void fullUpdate(); void fullUpdate();
void checkVisibility(); void checkVisibility();
void scrollToBottom(); void scrollToBottom();
void showContextMenu(const QPoint& globalPos, const QPointF& scenePos);
virtual void mousePressEvent(QMouseEvent* ev); virtual void mousePressEvent(QMouseEvent* ev);
virtual void mouseReleaseEvent(QMouseEvent* ev); virtual void mouseReleaseEvent(QMouseEvent* ev);

View File

@ -50,12 +50,12 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
avatar = new MaskablePixmapWidget(this, QSize(40,40), ":/img/avatar_mask.png"); avatar = new MaskablePixmapWidget(this, QSize(40,40), ":/img/avatar_mask.png");
QHBoxLayout *headLayout = new QHBoxLayout(), QHBoxLayout *headLayout = new QHBoxLayout(),
*mainFootLayout = new QHBoxLayout(); *mainFootLayout = new QHBoxLayout();
QVBoxLayout *mainLayout = new QVBoxLayout(), QVBoxLayout *mainLayout = new QVBoxLayout(),
*footButtonsSmall = new QVBoxLayout(), *footButtonsSmall = new QVBoxLayout(),
*volMicLayout = new QVBoxLayout(); *volMicLayout = new QVBoxLayout();
headTextLayout = new QVBoxLayout(); headTextLayout = new QVBoxLayout();
chatWidget = new ChatLog(this); chatWidget = new ChatLog(this);
@ -79,7 +79,7 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
//volButton->setFixedSize(25,20); //volButton->setFixedSize(25,20);
volButton->setToolTip(tr("Toggle speakers volume: RED is OFF")); volButton->setToolTip(tr("Toggle speakers volume: RED is OFF"));
micButton = new QPushButton(); micButton = new QPushButton();
// micButton->setFixedSize(25,20); // micButton->setFixedSize(25,20);
micButton->setToolTip(tr("Toggle microphone: RED is OFF")); micButton->setToolTip(tr("Toggle microphone: RED is OFF"));
footButtonsSmall->setSpacing(2); footButtonsSmall->setSpacing(2);
@ -122,7 +122,7 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
headTextLayout->addStretch(); headTextLayout->addStretch();
headTextLayout->addWidget(nameLabel); headTextLayout->addWidget(nameLabel);
volMicLayout->addWidget(micButton, Qt::AlignTop); volMicLayout->addWidget(micButton, Qt::AlignTop);
volMicLayout->addSpacing(2); volMicLayout->addSpacing(2);
volMicLayout->addWidget(volButton, Qt::AlignBottom); volMicLayout->addWidget(volButton, Qt::AlignBottom);
@ -143,13 +143,17 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked())); menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy"), this, SLOT(onCopyLogClicked()));
connect(chatWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint))); menu.addSeparator();
menu.addAction(QIcon::fromTheme("document-save"), tr("Save chat log"), this, SLOT(onSaveLogClicked()));
menu.addAction(QIcon::fromTheme("edit-clear"), tr("Clear displayed messages"), this, SLOT(clearChatArea(bool)));
menu.addSeparator();
connect(emoteButton, &QPushButton::clicked, this, &GenericChatForm::onEmoteButtonClicked);
connect(chatWidget, &ChatLog::customContextMenuRequested, this, &GenericChatForm::onChatContextMenuRequested);
chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css")); chatWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatArea.css"));
headWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatHead.css")); headWidget->setStyleSheet(Style::getStylesheet(":/ui/chatArea/chatHead.css"));
//ChatAction::setupFormat();
} }
bool GenericChatForm::isEmpty() bool GenericChatForm::isEmpty()
@ -180,6 +184,10 @@ void GenericChatForm::onChatContextMenuRequested(QPoint pos)
{ {
QWidget* sender = (QWidget*)QObject::sender(); QWidget* sender = (QWidget*)QObject::sender();
pos = sender->mapToGlobal(pos); pos = sender->mapToGlobal(pos);
//copy action
menu.actions().first()->setEnabled(chatWidget->hasTextToBeCopied());
menu.exec(pos); menu.exec(pos);
} }
@ -243,6 +251,25 @@ void GenericChatForm::onEmoteInsertRequested(QString str)
msgEdit->setFocus(); // refocus so that we can continue typing msgEdit->setFocus(); // refocus so that we can continue typing
} }
void GenericChatForm::onSaveLogClicked()
{
QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
file.write(chatWidget->toPlainText().toUtf8());
file.close();
}
void GenericChatForm::onCopyLogClicked()
{
chatWidget->copySelectedText();
}
void GenericChatForm::focusInput() void GenericChatForm::focusInput()
{ {
msgEdit->setFocus(); msgEdit->setFocus();

View File

@ -71,6 +71,8 @@ protected slots:
void onChatContextMenuRequested(QPoint pos); void onChatContextMenuRequested(QPoint pos);
void onEmoteButtonClicked(); void onEmoteButtonClicked();
void onEmoteInsertRequested(QString str); void onEmoteInsertRequested(QString str);
void onSaveLogClicked();
void onCopyLogClicked();
void clearChatArea(bool); void clearChatArea(bool);
protected: protected: