1 /* This file is part of the KDE project
2    Copyright (C) 2002, 2003 Laurent Montel <lmontel@mandrakesoft.com>
3    Copyright (C) 2006-2007,2010-2011 Jan Hambrecht <jaham@gmx.net>
4    Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20 */
21 
22 #include "KoConfigMiscPage.h"
23 
24 #include <KoUnit.h>
25 #include <KoDocument.h>
26 #include <KoUnitDoubleSpinBox.h>
27 #include <KoDocumentResourceManager.h>
28 #include <KoPart.h>
29 #include <KoComponentData.h>
30 
31 #include <kcombobox.h>
32 
33 #include <kconfig.h>
34 #include <kconfiggroup.h>
35 
36 #include <QFormLayout>
37 #include <QGroupBox>
38 #include <QLabel>
39 #include <QCheckBox>
40 #include <QSpinBox>
41 
42 class Q_DECL_HIDDEN KoConfigMiscPage::Private
43 {
44 public:
Private(KoDocument * doc,KoDocumentResourceManager * documentResources)45     Private(KoDocument* doc, KoDocumentResourceManager *documentResources)
46             : doc(doc), docResources(documentResources)
47     {}
48 
49     KoDocument* doc;
50     KSharedConfigPtr config;
51     KoDocumentResourceManager *docResources;
52 
53     KoUnit oldUnit;
54     QComboBox *unit;
55     QSpinBox *handleRadius;
56     uint oldHandleRadius;
57     QSpinBox *grabSensitivity;
58     uint oldGrabSensitivity;
59     KoUnitDoubleSpinBox* pasteOffset;
60     qreal oldPasteOffset;
61     QCheckBox *pasteAtCursor;
62     bool oldPasteAtCursor;
63 };
64 
KoConfigMiscPage(KoDocument * doc,KoDocumentResourceManager * documentResources,char * name)65 KoConfigMiscPage::KoConfigMiscPage(KoDocument* doc, KoDocumentResourceManager *documentResources, char* name)
66         : d(new Private(doc, documentResources))
67 {
68     setObjectName(name);
69 
70     d->config = d->doc->documentPart()->componentData().config();
71 
72     d->oldGrabSensitivity = d->docResources->grabSensitivity();
73     d->oldHandleRadius = d->docResources->handleRadius();
74     d->oldPasteOffset = d->docResources->pasteOffset();
75     d->oldPasteAtCursor = d->docResources->pasteAtCursor();
76 
77     const KoUnit documentUnit = d->doc->unit();
78 
79     QGroupBox *miscGroupBox = new QGroupBox(i18n("Misc"), this);
80 
81     QFormLayout *miscLayout = new QFormLayout();
82 
83     //#################"laurent
84     //don't load unitType from config file because unit is
85     //depend from words file => unit can be different from config file
86 
87     d->unit = new KComboBox(miscGroupBox);
88     d->unit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel));
89     miscLayout->addRow(i18n("Units:"), d->unit);
90     d->oldUnit = documentUnit;
91     d->unit->setCurrentIndex(d->oldUnit.indexInListForUi(KoUnit::HidePixel));
92 
93     d->handleRadius = new QSpinBox(miscGroupBox);
94     d->handleRadius->setRange(3, 20);
95     d->handleRadius->setSingleStep(1);
96     d->handleRadius->setSuffix(" px");
97     d->handleRadius->setValue(d->oldHandleRadius);
98     miscLayout->addRow(i18n("Handle radius:"), d->handleRadius);
99 
100     d->grabSensitivity = new QSpinBox(miscGroupBox);
101     d->grabSensitivity->setRange(3, 20);
102     d->grabSensitivity->setSingleStep(1);
103     d->grabSensitivity->setSuffix(" px");
104     d->grabSensitivity->setValue(d->oldGrabSensitivity);
105     miscLayout->addRow(i18n("Grab sensitivity:"), d->grabSensitivity);
106 
107     d->pasteOffset = new KoUnitDoubleSpinBox(miscGroupBox);
108     d->pasteOffset->setMinMaxStep(-1000, 1000, 0.1);
109     d->pasteOffset->setValue(d->oldPasteOffset);
110     d->pasteOffset->setUnit(documentUnit);
111     d->pasteOffset->setDisabled(d->oldPasteAtCursor);
112     miscLayout->addRow(i18n("Paste offset:"), d->pasteOffset);
113 
114     d->pasteAtCursor = new QCheckBox(miscGroupBox);
115     d->pasteAtCursor->setChecked(d->oldPasteAtCursor);
116     miscLayout->addRow(i18n("Paste at Cursor:"), d->pasteAtCursor);
117 
118     miscGroupBox->setLayout(miscLayout);
119 
120     connect(d->unit, SIGNAL(activated(int)), SLOT(slotUnitChanged(int)));
121     connect(d->pasteAtCursor, SIGNAL(clicked(bool)), d->pasteOffset, SLOT(setDisabled(bool)));
122 }
123 
~KoConfigMiscPage()124 KoConfigMiscPage::~KoConfigMiscPage()
125 {
126     delete d;
127 }
128 
apply()129 void KoConfigMiscPage::apply()
130 {
131     KConfigGroup miscGroup = d->config->group("Misc");
132 
133     int currentUnitIndex = d->unit->currentIndex();
134     if (d->oldUnit.indexInListForUi(KoUnit::HidePixel) != currentUnitIndex) {
135         d->oldUnit = KoUnit::fromListForUi(currentUnitIndex, KoUnit::HidePixel);
136         d->doc->setUnit(d->oldUnit);
137         miscGroup.writeEntry("Units", d->oldUnit.symbol());
138     }
139 
140     uint currentHandleRadius = d->handleRadius->value();
141     if (currentHandleRadius != d->oldHandleRadius) {
142         miscGroup.writeEntry( "HandleRadius", currentHandleRadius );
143         d->docResources->setHandleRadius(currentHandleRadius);
144     }
145 
146     uint currentGrabSensitivity = d->grabSensitivity->value();
147     if (currentGrabSensitivity != d->oldGrabSensitivity) {
148         miscGroup.writeEntry("GrabSensitivity", currentGrabSensitivity);
149         d->docResources->setGrabSensitivity(currentGrabSensitivity);
150     }
151 
152     qreal currentCopyOffset = d->pasteOffset->value();
153     if (currentCopyOffset != d->oldPasteOffset) {
154         miscGroup.writeEntry("CopyOffset", currentCopyOffset);
155         d->docResources->setPasteOffset(currentCopyOffset);
156     }
157 
158     const bool currentPasteAtCursor = d->pasteAtCursor->isChecked();
159     if (currentPasteAtCursor != d->oldPasteAtCursor) {
160         miscGroup.writeEntry("PasteAtCursor", currentPasteAtCursor);
161         d->docResources->enablePasteAtCursor(currentPasteAtCursor);
162     }
163 }
164 
slotDefault()165 void KoConfigMiscPage::slotDefault()
166 {
167     d->unit->setCurrentIndex(0);
168 }
169 
slotUnitChanged(int u)170 void KoConfigMiscPage::slotUnitChanged(int u)
171 {
172     const KoUnit unit = KoUnit::fromListForUi(u, KoUnit::HidePixel);
173 
174     d->pasteOffset->blockSignals(true);
175     d->pasteOffset->setUnit(unit);
176     d->pasteOffset->blockSignals(false);
177 
178     emit unitChanged(unit);
179 }
180