Subversion Repositories Scribus

Rev

Rev 17726 | Rev 20149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15635 fschmid 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
/**************************************************************************
8
*   Copyright (C) 2010 by Franz Schmid                                    *
9
*   franz.schmid@altmuehlnet.de                                           *
10
*                                                                         *
11
*   This program is free software; you can redistribute it and/or modify  *
12
*   it under the terms of the GNU General Public License as published by  *
13
*   the Free Software Foundation; either version 2 of the License, or     *
14
*   (at your option) any later version.                                   *
15
*                                                                         *
16
*   This program is distributed in the hope that it will be useful,       *
17
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
*   GNU General Public License for more details.                          *
20
*                                                                         *
21
*   You should have received a copy of the GNU General Public License     *
22
*   along with this program; if not, write to the                         *
23
*   Free Software Foundation, Inc.,                                       *
18122 mrdocs 24
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
15635 fschmid 25
***************************************************************************/
26
#include "swatchcombo.h"
15696 fschmid 27
#include <QFontMetrics>
28
#include <QFileInfo>
15635 fschmid 29
 
30
SwatchCombo::SwatchCombo( QWidget* parent ) : QToolButton(parent)
31
{
32
        dataTree = new QTreeWidget(NULL);
33
        dataTree->setHeaderHidden(true);
34
        dataTree->setMinimumSize(QSize(140, 200));
35
        popUp = new QMenu();
36
        popUpAct = new QWidgetAction(this);
37
        popUpAct->setDefaultWidget(dataTree);
38
        popUp->addAction(popUpAct);
39
        setMenu(popUp);
40
        setPopupMode(QToolButton::MenuButtonPopup);
41
        setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
42
        connect(dataTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(itemActivated(QTreeWidgetItem*)));
43
}
44
 
45
void SwatchCombo::itemActivated(QTreeWidgetItem* item)
46
{
47
        if (item)
48
        {
15681 fschmid 49
                if (item->flags() & Qt::ItemIsSelectable)
15635 fschmid 50
                {
51
                        menu()->hide();
52
                        setText(item->text(0));
15696 fschmid 53
                        QFontMetrics fm(font());
54
                        QString elText = fm.elidedText(item->text(0), Qt::ElideMiddle, width());
55
                        setText(elText);
56
                        setToolTip(item->text(0));
15635 fschmid 57
                        emit activated(item->text(0));
15696 fschmid 58
                        emit activated(item);
15635 fschmid 59
                }
60
        }
61
}
62
 
63
QTreeWidgetItem* SwatchCombo::addTopLevelItem(QString name)
64
{
65
        QTreeWidgetItem* item = new QTreeWidgetItem(dataTree);
66
        item->setFlags(Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
67
        item->setText(0, name);
68
        return item;
69
}
70
 
71
QTreeWidgetItem* SwatchCombo::addSubItem(QString name, QTreeWidgetItem* parent, bool selectable)
72
{
73
        QTreeWidgetItem* item = new QTreeWidgetItem(parent);
74
        if (selectable)
75
                item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
76
        else
77
                item->setFlags(Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
78
        item->setText(0, name);
79
        return item;
80
}
81
 
82
void SwatchCombo::setCurrentComboItem(QString text)
83
{
15696 fschmid 84
        QFontMetrics fm(font());
85
        setToolTip(text);
15764 fschmid 86
        QList<QTreeWidgetItem*> lg;
17726 jghali 87
 
15764 fschmid 88
        if (text == "Scribus Small")
15635 fschmid 89
        {
15764 fschmid 90
                QString elText = fm.elidedText(text, Qt::ElideMiddle, width());
91
                setText(elText);
92
                lg = dataTree->findItems(text, Qt::MatchExactly | Qt::MatchRecursive);
93
                dataTree->setCurrentItem(lg[0]);
94
                return;
95
        }
17726 jghali 96
 
97
        QFileInfo fo(text);
98
        QString txt = fo.baseName();
99
        lg = dataTree->findItems(txt, Qt::MatchExactly | Qt::MatchRecursive);
100
        if (lg.count() <= 0) return;
101
 
102
        QString dText = fo.absolutePath();
103
        for (int a = 0; a < lg.count(); a++)
15764 fschmid 104
        {
17726 jghali 105
                if (dText == lg[a]->data(0, Qt::UserRole).toString())
15696 fschmid 106
                {
17726 jghali 107
                        dataTree->setCurrentItem(lg[0]);
108
                        QString elText = fm.elidedText(txt, Qt::ElideMiddle, width());
109
                        setText(elText);
110
                        break;
15696 fschmid 111
                }
15635 fschmid 112
        }
17726 jghali 113
 
114
        if(!dataTree->currentItem())
115
                setCurrentComboItem("Scribus Small");
15635 fschmid 116
}
117
 
118
QTreeWidgetItem* SwatchCombo::currentItem()
119
{
120
        return dataTree->currentItem();
121
}