File: scimlistbox.cpp

package info (click to toggle)
skim 1.4.4-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,876 kB
  • ctags: 1,247
  • sloc: cpp: 9,421; python: 1,608; sh: 260; makefile: 68
file content (134 lines) | stat: -rw-r--r-- 4,596 bytes parent folder | download
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
/***************************************************************************
 *   Copyright (C) 2003-2005 by liuspider                                  *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#include "scimlistbox.h"
#include <qvariant.h>
#include <qlayout.h>
#include <qlabel.h>

#include <qwidget.h>
#include <qsize.h>

ScimListBox::ScimListBox(QWidget *parent, const char *name, bool _isVertical)
    : ScimDragableFrame(parent, parent, name), curSelection(0)
{
    setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum, 0, 0) );

    QBoxLayout::Direction defaultLayoutDirection = QBoxLayout::LeftToRight;

    if(_isVertical)
        defaultLayoutDirection = QBoxLayout::TopToBottom;

    layout = new QBoxLayout( this, defaultLayoutDirection, 3, 2, "defaultLayout");
    layout->setResizeMode( QLayout::FreeResize );

    for(int i=0; i < MAX_CANDIDATES; i++)
    {
        candidatesLbl[i] = new ScimStringListItem(this);
        candidatesLbl[i]->setFrameStyle( QFrame::NoFrame );
        candidatesLbl[i]->hide();
        candidatesLbl[i]->installEventFilter(this);
        candidatesLblIndexHash[candidatesLbl[i]] = i;
        layout->addWidget(candidatesLbl[i]);
    }
}

bool ScimListBox::eventFilter ( QObject * o, QEvent * e )
{
    if(candidatesLblIndexHash.contains(o))
        if(e->type() == QEvent::MouseButtonPress)
        {
            emit itemSelected(candidatesLblIndexHash[o]);
        }

    return false;
}

void ScimListBox::setVertical(bool b)
{
    QBoxLayout::Direction defaultLayoutDirection = QBoxLayout::LeftToRight;
    if(b)
        defaultLayoutDirection = QBoxLayout::TopToBottom;
    layout->setDirection(defaultLayoutDirection);
}

unsigned ScimListBox::updateContent( const QStringList & labellist,
                                     const QStringList & lookupstringlist,
                                     QValueList<scim::AttributeList> & attrslist,
                                     const bool pagesizefixed)
{
    static int max_width = m_screen.width() / 3;
    unsigned allitems = lookupstringlist.size();
    unsigned counter = 0;
    static QString labelSeparator, separator = " ";
    if(QBoxLayout::LeftToRight == layout->direction()) {
        labelSeparator = ".";
    } else {
        labelSeparator = ". ";
    }

    QString cache;
    scim::AttributeList attrs;
    scim::Attribute highlight;
    highlight.set_type(scim::SCIM_ATTR_DECORATE);
    highlight.set_value(scim::SCIM_ATTR_DECORATE_HIGHLIGHT);
    bool insertHighlight = false;

    static int cachedWidth = 0, curwidth;

    cachedWidth = 0;
    for(; counter < allitems && counter < MAX_CANDIDATES; counter++)
    {
        if(labellist[counter].isEmpty())
            cache = "";
        else    //only add the label if it is not empty
            cache = labellist[counter] + labelSeparator;

        attrs.clear();
        insertHighlight = (curSelection == counter);

        if(insertHighlight)
        {
            highlight.set_start(0);
            highlight.set_length(cache.length() + lookupstringlist[counter].length());
            attrs.push_back(highlight);
        }

        for(size_t j = 0; j < attrslist[counter].size(); j++)
        {
            attrslist[counter][j].set_start(attrslist[counter][j].get_start() + cache.length());
            attrs.push_back(attrslist[counter][j]);
        }
        candidatesLbl[counter]->setText(cache + lookupstringlist[counter] + separator, attrs);
        candidatesLbl[counter]->show();
        if(layout->direction() != QBoxLayout::TopToBottom && !pagesizefixed)
        {
            curwidth = candidatesLbl[counter]->minimumSizeHint().width();
            if(cachedWidth + curwidth > max_width)
            {
                counter++;
                break;
            }
            else
                cachedWidth += curwidth;
        }
    }

    for(int leftItems = counter; leftItems < MAX_CANDIDATES; leftItems++)
        candidatesLbl[leftItems]->hide();

    return counter;
}

void ScimListBox::updateHighlight(int i)
{
    curSelection = i;
}

#include "scimlistbox.moc"