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

@ -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: