summaryrefslogtreecommitdiffstats
path: root/konversation/src/viewtreeitem.cpp
blob: 28393669b3137a6c6716aa41eca8065445576e06 (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  Copyright (C) 2006 Eike Hein <hein@kde.org>
*/

#include "viewtreeitem.h"
#include "konversationapplication.h"
#include "chatwindow.h"
#include "preferences.h"
#include "images.h"


#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqimage.h>
#include <tqtimer.h>

#include <kdebug.h>
#include <kglobalsettings.h>
#include <kstringhandler.h>


int ViewTreeItem::s_availableSortIndex = 0;

ViewTreeItem::ViewTreeItem(TQListView* parent, const TQString& name, ChatWindow* view)
    : TQListViewItem(parent, name)
{
    m_sortIndex = s_availableSortIndex;
    s_availableSortIndex++;

    setView(view);
    setViewType(view->getType());

    m_color = KGlobalSettings::textColor();
    m_customColorSet = false;

    setOpen(true);
    setDragEnabled(true);

    m_isSeparator = false;
    m_isHighlighted = false;
    m_isTruncated = false;

    images = KonversationApplication::instance()->images();
    m_closeButtonShown = false;
    m_closeButtonEnabled = false;
}

ViewTreeItem::ViewTreeItem(TQListViewItem* parent, const TQString& name, ChatWindow* view, int sortIndex)
    : TQListViewItem(parent, name)
{
    if (sortIndex != -1)
        setSortIndex(sortIndex);
    else
    {
        m_sortIndex = s_availableSortIndex;
        s_availableSortIndex++;
    }

    setView(view);
    setViewType(view->getType());

    m_color = KGlobalSettings::textColor();
    m_customColorSet = false;

    setOpen(true);
    setDragEnabled(true);

    m_isSeparator = false;
    m_isHighlighted = false;
    m_isTruncated = false;
    m_customColorSet = false;

    images = KonversationApplication::instance()->images();
    m_closeButtonShown = false;
    m_closeButtonEnabled = false;
}

ViewTreeItem::ViewTreeItem(TQListViewItem* parent, TQListViewItem* afterItem, const TQString& name, ChatWindow* view)
    : TQListViewItem(parent, afterItem, name)
{
    m_sortIndex = s_availableSortIndex;
    s_availableSortIndex++;

    setView(view);
    setViewType(view->getType());

    m_color = KGlobalSettings::textColor();
    m_customColorSet = false;

    setOpen(true);
    setDragEnabled(true);

    m_isSeparator = false;
    m_isHighlighted = false;
    m_isTruncated = false;
    m_customColorSet = false;

    images = KonversationApplication::instance()->images();
    m_closeButtonShown = false;
    m_closeButtonEnabled = false;
}

ViewTreeItem::ViewTreeItem(TQListView* parent) : TQListViewItem(parent)
{
    setView(0);
    setOpen(true);
    setDragEnabled(false);

    m_isSeparator = true;
    m_isHighlighted = false;
    m_isTruncated = false;
}

ViewTreeItem::~ViewTreeItem()
{
}

void ViewTreeItem::setSortIndex(int newSortIndex)
{
    m_sortIndex = newSortIndex;
}

int ViewTreeItem::getSortIndex() const
{
    return m_sortIndex;
}

void ViewTreeItem::setName(const TQString& name)
{
    setText(0, name);
}

TQString ViewTreeItem::getName() const
{
    return text(0);
}

bool ViewTreeItem::isTruncated() const
{
    return m_isTruncated;
}

void ViewTreeItem::setView(ChatWindow* view)
{
    m_view = view;
}

ChatWindow* ViewTreeItem::getView() const
{
    return m_view;
}

void ViewTreeItem::setViewType(ChatWindow::WindowType viewType)
{
    m_viewType = viewType;
}

ChatWindow::WindowType ViewTreeItem::getViewType() const
{
    return m_viewType;
}

void ViewTreeItem::setColor(TQColor color)
{
    if (color != m_color)
    {
        m_color = color;
        m_customColorSet = true;
        repaint();
    }
}

TQColor ViewTreeItem::getColor() const
{
    if (!m_customColorSet)
    {
        if (Preferences::inputFieldsBackgroundColor())
            return Preferences::color(Preferences::ChannelMessage);
        else
            return KGlobalSettings::textColor();
    }
    else
        return m_color;
}

void ViewTreeItem::setIcon(const TQPixmap& pm)
{
    m_oldPixmap = pm;
    if (!m_closeButtonShown) setPixmap(0, pm);
}

void ViewTreeItem::setHighlighted(bool highlight)
{
    if (m_isHighlighted != highlight)
    {
        m_isHighlighted = highlight;
        repaint();
    }
}

void ViewTreeItem::setCloseButtonShown(bool show)
{
    if (!show && !m_closeButtonShown)
        return;

    if (show && m_closeButtonShown)
        return;

    if (show && !m_closeButtonShown)
    {
        setPixmap(0, images->getDisabledCloseIcon());
        m_closeButtonShown = true;
        m_closeButtonEnabled = false;
    }

    if (!show && m_closeButtonShown)
    {
        setPixmap(0, m_oldPixmap);
        m_closeButtonShown = false;
        m_closeButtonEnabled = false;
    }
}

void ViewTreeItem::setCloseButtonEnabled()
{
    if (m_closeButtonShown)
    {
        m_closeButtonEnabled = true;
        setPixmap(0, images->getCloseIcon());
    }
}

bool ViewTreeItem::getCloseButtonEnabled()
{
    return m_closeButtonEnabled;
}

bool ViewTreeItem::sortLast() const
{
    if (!m_isSeparator)
    {
        if (getViewType() == ChatWindow::Status
            || getViewType() == ChatWindow::Channel
            || getViewType() == ChatWindow::Query
            || getViewType() == ChatWindow::RawLog
            || getViewType() == ChatWindow::DccChat
            || getViewType() == ChatWindow::ChannelList)
        {
            return false;
        }
        else
            return true;
    }
    else
    {
        return true;
    }
}

bool ViewTreeItem::isSeparator() const
{
    return m_isSeparator;
}

int ViewTreeItem::compare(TQListViewItem *i, int /* col */, bool /* ascending */) const
{
    ViewTreeItem* item = static_cast<ViewTreeItem*>(i);

    if (sortLast() == item->sortLast())
    {
        if (isSeparator() && !item->isSeparator())
            return -1;
        if (!isSeparator() && item->isSeparator())
            return 1;
        else
        {
            if (getSortIndex() == item->getSortIndex())
                return 0;
            else if (getSortIndex() < item->getSortIndex())
                return -1;
            else
                return 1;
        }
    }
    else if (sortLast())
        return 1;
    else
        return -1;
}

void ViewTreeItem::setup()
{
    widthChanged();

    if (!m_isSeparator)
    {
        int LED_ICON_SIZE = 14;
        int MARGIN = 2;

        TQRect textRect = listView()->fontMetrics().boundingRect(0, 0, /*width=*/1, 500000, TQt::AlignAuto | TQt::AlignTop | TQt::ShowPrefix, text(/*column=*/0));
        int height = MARGIN + kMax(LED_ICON_SIZE, textRect.height()) + MARGIN;
        setHeight(height);
    }
    else
        setHeight(11);
}

TQColor ViewTreeItem::mixColor(const TQColor &color1, const TQColor &color2)
{
    TQColor mixedColor;
    mixedColor.setRgb( (color1.red()   + color2.red())   / 2,
                       (color1.green() + color2.green()) / 2,
                       (color1.blue()  + color2.blue())  / 2 );
    return mixedColor;
}

void ViewTreeItem::paintFocus(TQPainter* /* p */, const TQColorGroup& /* cg */, const TQRect& /* r */)
{
    // Do nothing.
    return;
}

void ViewTreeItem::paintCell(TQPainter* p, const TQColorGroup& /* cg */, int /* column */, int width, int /* align */)
{
    // Workaround a TQt bug:
    // When the splitter is moved to hide the tree view and then the application is restarted,
    // TQt try to draw items with a negative size.
    if (width <= 0) return;

    int LED_ICON_SIZE = 14;
    int MARGIN = 2;

    // Bufferize the drawing of items.
    TQPixmap buffer(width, height());
    TQPainter painter(&buffer);

    TQColor textColor = isSelected() ? KGlobalSettings::highlightedTextColor() : getColor();
    TQColor background = isSelected() ? KGlobalSettings::highlightColor() : listView()->paletteBackgroundColor();
    if (m_isHighlighted) background = Preferences::inputFieldsBackgroundColor()
        ? Preferences::color(Preferences::AlternateBackground) : KGlobalSettings::alternateBackgroundColor();

    // Fill in background.
    painter.fillRect(0, 0, width, height(), background);

    TQColor bgColor  = listView()->paletteBackgroundColor();
    TQColor selColor = m_isHighlighted ? background : KGlobalSettings::highlightColor();
    TQColor midColor = mixColor(bgColor, selColor);

    int iconWidth = pixmap(0) ? LED_ICON_SIZE : 0;
    int textWidth = width - MARGIN - iconWidth - MARGIN - MARGIN;

    if (!m_isSeparator)
    {
        // Draw the rounded rectangle.
        TQRect textRect = listView()->fontMetrics().boundingRect(0, 0, /*width=*/1, 500000, TQt::AlignAuto | TQt::AlignTop | TQt::ShowPrefix, text(/*column=*/0));
        int xRound = MARGIN;
        int yRound = MARGIN;
        int hRound = height() - 2 * MARGIN;
        int wRound = kMin(LED_ICON_SIZE + MARGIN + textRect.width() + hRound/2,  width - MARGIN - MARGIN);

        if (wRound > 0)
        {
            TQPixmap buffer(wRound * 2, hRound * 2);
            buffer.fill(background);
            TQPainter pBuffer(&buffer);
            TQColor colorRound = background;
            pBuffer.setPen(colorRound);
            pBuffer.setBrush(colorRound);

            // If the rectangle is higher than wide, don't overlap ellipses.
            if (wRound > hRound)
            {
                pBuffer.drawEllipse(0,                                 0, hRound * 2, hRound * 2);
                pBuffer.drawEllipse(wRound * 2 - hRound * 2, 0, hRound * 2, hRound * 2);
                pBuffer.fillRect(hRound*2/2, 0, wRound * 2 - hRound * 2, hRound * 2, colorRound);
            }
            else
                pBuffer.drawEllipse(0, 0, wRound * 2, hRound * 2);

            pBuffer.end();
            TQImage imageToScale = buffer.convertToImage();
            TQPixmap pmScaled;
            pmScaled.convertFromImage(imageToScale.smoothScale(wRound, hRound));
            painter.drawPixmap(xRound, yRound, pmScaled);
            textWidth -= hRound/2;
        }

        if (isSelected() || m_isHighlighted)
        {
            painter.setPen(bgColor);
            painter.drawPoint(0, 0);
            painter.drawPoint(1, 0);
            painter.drawPoint(0, 1);
            painter.drawPoint(0, height() - 1);
            painter.drawPoint(1, height() - 1);
            painter.drawPoint(0, height() - 2);
            painter.setPen(midColor);
            painter.drawPoint(2, 0);
            painter.drawPoint(0, 2);
            painter.drawPoint(2, height() - 1);
            painter.drawPoint(0, height() - 3);
        }
    }

    if (m_isHighlighted)
    {
        selColor = KGlobalSettings::highlightColor();
        midColor = mixColor(bgColor, selColor);
    }

    if (itemBelow() && itemBelow()->isSelected())
    {
        painter.setPen(selColor);
        painter.drawPoint(width - 1, height() - 1);
        painter.drawPoint(width - 2, height() - 1);
        painter.drawPoint(width - 1, height() - 2);
        painter.setPen(midColor);
        painter.drawPoint(width - 3, height() - 1);
        painter.drawPoint(width - 1, height() - 3);
    }

    if (itemAbove() && itemAbove()->isSelected())
    {
        painter.setPen(selColor);
        painter.drawPoint(width - 1, 0);
        painter.drawPoint(width - 2, 0);
        painter.drawPoint(width - 1, 1);
        painter.setPen(midColor);
        painter.drawPoint(width - 3, 0);
        painter.drawPoint(width - 1, 2);
    }

    if (!m_isSeparator)
    {
        // Draw icon.
        if (pixmap(0))
        {
            int yPixmap = (height() - pixmap(0)->height()) / 2;

            int xPixmap = MARGIN;

            if (pixmap(0)->width() < LED_ICON_SIZE)
                xPixmap  = MARGIN + ((LED_ICON_SIZE - pixmap(0)->width()) / 2);

            painter.drawPixmap(xPixmap, yPixmap, *pixmap(0));
        }

        // Enough space left to draw icon and text?
        if (textWidth > 0)
        {
            int xText = MARGIN;

            // Shift by icon width.
            if (pixmap(0))
                xText = MARGIN + LED_ICON_SIZE + MARGIN;

            TQString text = getName();

            if (p->fontMetrics().width(text) > textWidth)
            {
                m_isTruncated = true;
                text = KStringHandler::rPixelSqueeze(text, p->fontMetrics(), textWidth);
            }
            else
                m_isTruncated = false;

            painter.setPen(textColor);
            painter.setFont(listView()->font());
            painter.drawText(xText, 0, textWidth, height(), TQt::AlignAuto | TQt::AlignVCenter, text);
        }
    }
    else
    {
        TQColor lineColor = Preferences::inputFieldsBackgroundColor()
            ? Preferences::color(Preferences::AlternateBackground) : KGlobalSettings::alternateBackgroundColor();
        painter.setPen(lineColor);
        painter.drawLine(0, 5, width, 5);
    }

    painter.end();

    p->drawPixmap(0, 0, buffer);
}