1 /*
2     This file is part of Knights, a chess board for KDE SC 4.
3     Copyright 2009,2010,2011  Miha Čančula <miha@noughmad.eu>
4 
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License as
7     published by the Free Software Foundation; either version 2 of
8     the License or (at your option) version 3 or any later version
9     accepted by the membership of KDE e.V. (or its successor approved
10     by the membership of KDE e.V.), which shall act as a proxy
11     defined in Section 14 of version 3 of the license.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "proto/chatwidget.h"
23 #include "ui_chatwidget.h"
24 
25 #include <QScrollBar>
26 
27 using namespace Knights;
28 
29 class ScrollBarPin {
30 public:
ScrollBarPin(QScrollBar * scrollBar)31 	ScrollBarPin(QScrollBar *scrollBar) : m_bar(scrollBar) {
32 		if (m_bar)
33 			m_bar = m_bar->value() == m_bar->maximum()? m_bar : nullptr;
34 	}
~ScrollBarPin()35 	~ScrollBarPin() {
36 		if (m_bar)
37 			m_bar->setValue(m_bar->maximum());
38 	}
39 private:
40 	QPointer<QScrollBar> m_bar;
41 };
42 
resizeEvent(QResizeEvent * event)43 void Terminal::resizeEvent ( QResizeEvent * event ) {
44 	ScrollBarPin b(verticalScrollBar());
45 	QTextBrowser::resizeEvent(event);
46 }
47 
ChatWidget(QWidget * parent,Qt::WindowFlags f)48 ChatWidget::ChatWidget ( QWidget* parent, Qt::WindowFlags f ) : QWidget ( parent, f ) {
49 	ui = new Ui::ChatWidget;
50 	ui->setupUi(this);
51 
52 	connect ( ui->sendButton, &QPushButton::clicked, this, &ChatWidget::sendButtonClicked );
53 	ui->sendButton->setIcon( QIcon::fromTheme(QStringLiteral("mail-send")) );
54 
55 	m_terminal = new Terminal;
56 	ui->consoleLayout->addWidget(m_terminal);
57 
58 	setConsoleMode ( false );
59 }
60 
~ChatWidget()61 ChatWidget::~ChatWidget() {
62 	delete ui;
63 }
64 
addText(const QString & text,ChatWidget::MessageType type)65 void ChatWidget::addText ( const QString& text, ChatWidget::MessageType type ) {
66 	ScrollBarPin b(m_terminal->verticalScrollBar());
67 	QTextCursor cursor = m_terminal->textCursor();
68 	QTextCharFormat format = cursor.charFormat();
69 	cursor.movePosition(QTextCursor::End);
70 	if ( type == ChatMessage && text.contains(QLatin1String(" says: ")) ) {
71 		format.setForeground( QBrush( messageColor ( StatusMessage ) ) );
72 		cursor.setCharFormat( format );
73 		cursor.insertText ( text.left ( text.indexOf(QLatin1String(" says: ")) ) );
74 		format.setForeground( QBrush( messageColor ( GeneralMessage ) ) );
75 		cursor.setCharFormat( format );
76 		cursor.insertText ( i18n(" says: ") );
77 		format.setForeground( QBrush( messageColor ( ChatMessage ) ) );
78 		cursor.setCharFormat( format );
79 		cursor.insertText ( text.mid ( text.indexOf(QLatin1String(" says: ")) + 7 ) );
80 	} else if (type == GreetMessage) {
81 		format.setFontItalic(true);
82 		format.setForeground( QBrush( messageColor ( type ) ) );
83 		cursor.setCharFormat( format );
84 		cursor.insertText( text );
85 	} else {
86 		format.setForeground( QBrush( messageColor ( type ) ) );
87 		cursor.setCharFormat( format );
88 		cursor.insertText( text );
89 	}
90 	cursor.insertText(QStringLiteral("\n"));
91 }
92 
addText(const QByteArray & text,ChatWidget::MessageType type)93 void ChatWidget::addText ( const QByteArray& text, ChatWidget::MessageType type ) {
94 	addText( QLatin1String(text), type );
95 }
96 
addText(const Message & message)97 void ChatWidget::addText(const Message& message) {
98 	addText ( message.first, message.second );
99 }
100 
setPasswordMode(bool pwMode)101 void ChatWidget::setPasswordMode ( bool pwMode ) {
102 	ui->line->setPasswordMode ( pwMode );
103 }
104 
sendButtonClicked()105 void ChatWidget::sendButtonClicked() {
106 	if ( m_consoleMode )
107 		addText ( ui->line->text(), GeneralMessage );
108 	else
109 		addText ( i18n("You: ") + ui->line->text(), ChatMessage );
110 	Q_EMIT sendText ( ui->line->text() );
111 	ui->line->clear();
112 }
113 
addExtraButton(const QString & text,const QString & title,const QString & icon)114 void ChatWidget::addExtraButton ( const QString& text, const QString& title, const QString& icon ) {
115 	QPushButton* button = new QPushButton ( this );
116 	if ( !title.isEmpty() )
117 		button->setText ( title );
118 	else
119 		button->setText ( text );
120 	if ( !icon.isEmpty() )
121 		button->setIcon ( QIcon::fromTheme(icon) );
122 	ui->extraButtonsLayout->addWidget ( button );
123 	m_extraButtons.insert ( button, text );
124 	connect ( button, &QPushButton::clicked, this, &ChatWidget::buttonClicked );
125 }
126 
buttonClicked()127 void ChatWidget::buttonClicked() {
128 	QObject* s = sender();
129 	if ( m_extraButtons.contains ( s ) )
130 		Q_EMIT sendText ( m_extraButtons[s] );
131 }
132 
messageColor(ChatWidget::MessageType type) const133 QColor ChatWidget::messageColor ( ChatWidget::MessageType type ) const {
134 	if ( m_colors.contains ( type ) )
135 		return m_colors[type];
136 	return m_consoleMode ? Qt::white : Qt::black;
137 }
138 
setMessageColor(ChatWidget::MessageType type,const QColor & color)139 void ChatWidget::setMessageColor ( ChatWidget::MessageType type, const QColor& color ) {
140 	m_colors[type] = color;
141 }
142 
setConsoleMode(bool console)143 void ChatWidget::setConsoleMode ( bool console ) {
144 	m_consoleMode = console;
145 	m_colors.clear();
146 	m_colors[AccountMessage] = Qt::magenta;
147 	m_colors[ErrorMessage] = Qt::red;
148 	m_colors[GreetMessage] = Qt::gray;
149 	m_colors[ChatMessage] = Qt::blue;
150 	m_colors[ChallengeMessage] = Qt::cyan;
151 
152 	if ( console ) {
153 		QPalette p = m_terminal->palette();
154 		p.setColor ( QPalette::Base, Qt::black );
155 		m_terminal->setPalette ( p );
156 
157 		m_colors[StatusMessage] = Qt::green;
158 		m_colors[SeekMessage] = Qt::yellow;
159 		m_colors[GeneralMessage] = Qt::white;
160 	} else {
161 		m_terminal->setPalette( palette() );
162 
163 		m_colors[StatusMessage] = Qt::darkGreen;
164 		m_colors[GeneralMessage] = Qt::black;
165 		m_colors[SeekMessage] = Qt::darkYellow;
166 	}
167 }
168 
consoleMode() const169 bool ChatWidget::consoleMode() const {
170 	return m_consoleMode;
171 }
172 
173 
174 
175 
176 
177 
178