1 /***************************************************************************
2  *   Copyright (c) 2009  Nikolaj Hald Nielsen <nhnFreespirit@gmail.com>    *
3  *   Copyright (c) 2009  Roman Jarosz         <kedgedev@gmail.com>         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 
21 #include "contactlistlayouteditwidget.h"
22 #include "TokenDropTarget.h"
23 #include "contactlistlayoutmanager.h"
24 #include "contactlisttoken.h"
25 
26 #include <KHBox>
27 #include <KLocalizedString>
28 #include <KDebug>
29 
30 #include <QCheckBox>
31 #include <QSpinBox>
32 
33 using namespace ContactList;
34 
LayoutEditWidget(QWidget * parent)35 LayoutEditWidget::LayoutEditWidget(QWidget *parent)
36     : KVBox(parent)
37 {
38     m_tokenFactory = new ContactListTokenFactory;
39     m_tokenDropTarget = new TokenDropTarget(QStringLiteral("application/x-kopete-contactlist-token"), this);
40     m_tokenDropTarget->setCustomTokenFactory(m_tokenFactory);
41     connect(m_tokenDropTarget, SIGNAL(focussed(QWidget*)), this, SIGNAL(focussed(QWidget*)));
42     connect(m_tokenDropTarget, SIGNAL(changed()), this, SIGNAL(changed()));
43 
44     m_showIconCheckBox = new QCheckBox(i18n("Show Icon"), this);
45     connect(m_showIconCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
46 }
47 
~LayoutEditWidget()48 LayoutEditWidget::~LayoutEditWidget()
49 {
50 //     delete m_tokenFactory; m_tokenFactory = 0;
51 }
52 
readLayout(ContactList::LayoutItemConfig config)53 void LayoutEditWidget::readLayout(ContactList::LayoutItemConfig config)
54 {
55     int rowCount = config.rows();
56 
57     m_showIconCheckBox->setChecked(config.showIcon());
58 
59     m_tokenDropTarget->clear();
60 
61     for (int i = 0; i < rowCount; i++) {
62         //get the row config
63         ContactList::LayoutItemConfigRow rowConfig = config.row(i);
64 
65         int elementCount = rowConfig.count();
66 
67         //FIXME! for now, each element get the same size. This needs extensions to the token stuff
68         //qreal size = 1.0 / (qreal) elementCount;
69 
70         for (int j = 0; j < elementCount; j++) {
71             ContactList::LayoutItemConfigRowElement element = rowConfig.element(j);
72             ContactList::ContactListTokenConfig clToken = ContactList::LayoutManager::instance()->token(element.value());
73             ContactListToken *token = new ContactListToken(clToken.mName, clToken.mIconName, element.value(), m_tokenDropTarget);
74             token->setBold(element.bold());
75             token->setSmall(element.small());
76             token->setOptimalSize(element.optimalSize());
77             token->setItalic(element.italic());
78             token->setAlignment(element.alignment());
79             m_tokenDropTarget->insertToken(token, i, j);
80             token->setWidth(element.size() * 100.0);
81         }
82     }
83 }
84 
config()85 ContactList::LayoutItemConfig LayoutEditWidget::config()
86 {
87     LayoutItemConfig config;
88     config.setShowIcon(m_showIconCheckBox->isChecked());
89 
90     int noOfRows = m_tokenDropTarget->rows();
91 
92     for (int i = 0; i < noOfRows; i++) {
93         LayoutItemConfigRow currentRowConfig;
94 
95         QList<Token *> tokens = m_tokenDropTarget->drags(i);
96 
97         foreach (Token *token, tokens) {
98             if (ContactListToken *twl = dynamic_cast<ContactListToken *>(token)) {
99                 qreal width = 0.0;
100                 if (twl->widthForced() && twl->width() > 0.01) {
101                     width = twl->width();
102                 }
103                 currentRowConfig.addElement(LayoutItemConfigRowElement(twl->value(), width, twl->bold(), twl->italic(), twl->small(),
104                                                                        twl->optimalSize(), twl->alignment(), twl->prefix(), twl->suffix()));
105             }
106         }
107 
108         config.addRow(currentRowConfig);
109     }
110     return config;
111 }
112