1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
This file is part of the KDE games library
Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
Copyright (C) 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kchatbase.h"
#include "kchatbasemodel.h"
#include "kchatbaseitemdelegate.h"
#include <KLineEdit>
#include <KLocalizedString>
#include <KConfig>
#include <QLayout>
#include <QComboBox>
#include <QPixmap>
#include <QList>
#include <QApplication>
#include <QListView>
Q_LOGGING_CATEGORY(GAMES_BACKGAMMON, "org.kde.games.backgammon", QtWarningMsg)
class KChatBasePrivate
{
public:
KChatBasePrivate(KChatBaseModel* model, KChatBaseItemDelegate* delegate)
{
mBox = nullptr;
mEdit = nullptr;
mCombo = nullptr;
mAcceptMessage = true;
mModel = model;
mDelegate = delegate;
}
QListView* mBox;
KLineEdit* mEdit;
QComboBox* mCombo;
bool mAcceptMessage;
QList<int> mIndex2Id;
KChatBaseModel* mModel;
KChatBaseItemDelegate* mDelegate;
};
void KChatBase::setModel(KChatBaseModel* m)
{
//delete d->mModel;
d->mModel=m;
}
KChatBaseModel* KChatBase::model()
{
return d->mModel;
}
KChatBase::KChatBase(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox) : QFrame(parent)
{
KChatBaseModel* mod = model;
if (mod==nullptr)
mod = new KChatBaseModel(parent);
KChatBaseItemDelegate* del = delegate;
if (del == nullptr)
del = new KChatBaseItemDelegate(parent);
d = new KChatBasePrivate(mod, del);
setMinimumWidth(100);
setMinimumHeight(150);
QVBoxLayout* l = new QVBoxLayout(this);
d->mBox = new QListView();
d->mBox->setModel(d->mModel);
d->mBox->setItemDelegate(d->mDelegate);
l->addWidget(d->mBox);
connect(d->mModel, &QAbstractItemModel::rowsInserted,
d->mBox, &QAbstractItemView::scrollToBottom);
connect(d->mBox, &QListView::customContextMenuRequested, this, &KChatBase::customMenuHandler);
d->mBox->setContextMenuPolicy ( Qt::CustomContextMenu );
d->mBox->setFocusPolicy(Qt::NoFocus);
d->mBox->setSelectionMode(QAbstractItemView::SingleSelection);
l->addSpacing(5);
QHBoxLayout* h = new QHBoxLayout;
l->addLayout(h);
d->mEdit = new KLineEdit(this);
d->mEdit->setHandleSignals(false);
d->mEdit->setTrapReturnKey(true);
d->mEdit->completionObject(); // add the completion object
d->mEdit->setCompletionMode(KCompletion::CompletionNone);
connect(d->mEdit, &KLineEdit::returnPressed, this, &KChatBase::slotReturnPressed);
h->addWidget(d->mEdit);
if (!noComboBox) {
d->mCombo = new QComboBox(this);
h->addWidget(d->mCombo);
addSendingEntry(i18n("Send to All Players"), SendToAll);//FIXME: where to put the id?
}
d->mAcceptMessage = true; // by default
setMaxItems(-1); // unlimited
readConfig();
}
const QModelIndex KChatBase::indexAt(const QPoint& pos) const
{
return d->mBox->indexAt(pos);
}
void KChatBase::customMenuHandler(const QPoint &pos)
{
qCDebug(GAMES_BACKGAMMON) << "custom menu has been requested at position="<<pos<<". Implement handler at subclass if you need it.";
}
KChatBase::~KChatBase()
{
// qCDebug(GAMES_LIB) << "KChatBase: DESTRUCT (" << this << ")";
saveConfig();
delete d;
}
bool KChatBase::acceptMessage() const
{ return d->mAcceptMessage; }
void KChatBase::setAcceptMessage(bool a)
{ d->mAcceptMessage = a; }
bool KChatBase::addSendingEntry(const QString& text, int id)
{
//FIXME: is ID used correctly?
// do we need ID at all?
// what the hell should be here?
// d->mCombo->insertItem(i18n("Send to All Players"), SendToAll);
return insertSendingEntry(text, id);
}
bool KChatBase::insertSendingEntry(const QString& text, int id, int index)
{
if (!d->mCombo) {
qCWarning(GAMES_LIB) << "KChatBase: Cannot add an entry to the combo box";
return false;
}
if (d->mIndex2Id.indexOf(id) != -1) {
qCCritical(GAMES_LIB) << "KChatBase: Cannot add more than one entry with the same ID! ";
qCCritical(GAMES_LIB) << "KChatBase: Text="<<text;
return false;
}
d->mCombo->insertItem(index, text);
if (index < 0) {
d->mIndex2Id.prepend(id);
} else {
d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
}
if (d->mIndex2Id.count() != d->mCombo->count()) {
qCCritical(GAMES_LIB) << "KChatBase: internal ERROR - local IDs do not match combo box entries!";
}
return true;
}
int KChatBase::sendingEntry() const
{
if (!d->mCombo) {
qCWarning(GAMES_PRIVATE_KGAME) << "Cannot retrieve index from NULL combo box";
return -1;
}
const int index = d->mCombo->currentIndex();
if ( index >= 0 && index < d->mIndex2Id.size())
return d->mIndex2Id[index];
qCWarning(GAMES_LIB) << "could not find the selected sending entry!";
return -1;
}
void KChatBase::removeSendingEntry(int id)
{
if (!d->mCombo) {
qCWarning(GAMES_LIB) << "KChatBase: Cannot remove an entry from the combo box";
return;
}
int idx = findIndex(id);
//Guard, passing -1 will crash qcombobox
if (idx>=0) d->mCombo->removeItem(idx);
d->mIndex2Id.removeAll(id);
}
void KChatBase::changeSendingEntry(const QString& text, int id)
{
if (!d->mCombo) {
qCWarning(GAMES_LIB) << "KChatBase: Cannot change an entry in the combo box";
return;
}
int index = findIndex(id);
d->mCombo->setItemText(index, text);
}
void KChatBase::setSendingEntry(int id)
{
if (!d->mCombo) {
qCWarning(GAMES_LIB) << "KChatBase: Cannot set an entry in the combo box";
return;
}
d->mCombo->setCurrentIndex(findIndex(id));
}
int KChatBase::findIndex(int id) const
{
return d->mIndex2Id.indexOf(id);
}
int KChatBase::nextId() const
{
int i = SendToAll + 1;
while (d->mIndex2Id.indexOf(i) != -1) {
i++;
}
return i;
}
void KChatBase::slotReturnPressed(const QString& text)
{
if (text.length() <= 0) {
// no text entered - probably hit return by accident
return;
} else if (!acceptMessage()) {
return;
}
d->mEdit->completionObject()->addItem(text);
d->mEdit->clear();
returnPressed(text);
}
QString KChatBase::comboBoxItem(const QString& name) const
{ // TODO: such a function for "send to all" and "send to my group"
return i18n("Send to %1", name);
}
void KChatBase::slotClear()
{
clear();
}
void KChatBase::setCompletionMode(KCompletion::CompletionMode mode)
{ d->mEdit->setCompletionMode(mode); }
void KChatBase::saveConfig(KConfig* conf)
{
if (conf == nullptr) {
return;
}
d->mModel->saveConfig(conf);
}
void KChatBase::readConfig(KConfig* conf)
{
if (conf == nullptr) {
return;
}
d->mModel->readConfig(conf);
}
void KChatBase::clear()
{
d->mModel->removeRows(0, d->mModel->rowCount());
}
void KChatBase::setMaxItems(int maxItems)
{
d->mModel->setMaxItems(maxItems);
//TODO cut too many messages
if (maxItems == 0) {
clear();
} else if (maxItems > 0) {
while (d->mModel->rowCount() > (int)maxItems) {
d->mModel->removeRow(0);
}
}
}
int KChatBase::maxItems() const
{
return d->mModel->maxItems();
}
QFont KChatBase::nameFont() const
{
return d->mModel->nameFont();
}
QFont KChatBase::messageFont() const
{
return d->mModel->messageFont();
}
QFont KChatBase::systemNameFont() const
{
return d->mModel->systemNameFont();
}
QFont KChatBase::systemMessageFont() const
{
return d->mModel->systemMessageFont();
}
void KChatBase::setNameFont(const QFont& font)
{
d->mModel->setNameFont(font);
}
void KChatBase::setMessageFont(const QFont& font)
{
d->mModel->setMessageFont(font);
}
void KChatBase::setBothFont(const QFont& font)
{
d->mModel->setBothFont(font);
}
void KChatBase::setSystemNameFont(const QFont& font)
{
d->mModel->setSystemNameFont(font);
}
void KChatBase::setSystemMessageFont(const QFont& font)
{
d->mModel->setSystemMessageFont(font);
}
void KChatBase::setSystemBothFont(const QFont& font)
{
d->mModel->setSystemBothFont(font);
}
void KChatBase::addMessage(const QString& fromName, const QString& text)
{
d->mModel->addMessage(fromName, text);
}
void KChatBase::addSystemMessage(const QString& fromName, const QString& text)
{
d->mModel->addSystemMessage(fromName, text);
}
|