summaryrefslogtreecommitdiffstats
path: root/src/dolphiniconsviewsettings.cpp
blob: da39391876cee6c7ccdb4070189d3c8e3737e799 (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
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz                                      *
 *   peter.penz@gmx.at                                                     *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "dolphiniconsviewsettings.h"
#include <kicontheme.h>
#include <tdeglobalsettings.h>
#include <tdeapplication.h>
#include <assert.h>

DolphinIconsViewSettings::DolphinIconsViewSettings(DolphinIconsView::LayoutMode mode) :
    m_arrangement(TQIconView::LeftToRight),
    m_layoutMode(mode),
    m_iconSize(TDEIcon::SizeMedium),
    m_previewSize(TDEIcon::SizeMedium),
    m_gridWidth(0),
    m_gridHeight(TDEIcon::SizeMedium),
    m_gridSpacing(8),
    m_fontSize(0),
    m_textlinesCount(2)
{
    TDEConfig* config = kapp->config();
    setConfigGroup(config);

    // read icon size
    m_iconSize = config->readNumEntry("Icon Size", -1);
    if (m_iconSize < 0) {
        m_iconSize = TDEIcon::SizeMedium;
    }

    // read arrangement
    const TQString arrangement(config->readEntry("Arrangement"));
    if (arrangement == "Left to Right") {
        m_arrangement = TQIconView::LeftToRight;
    }
    else if (arrangement == "Top to Bottom") {
        m_arrangement = TQIconView::TopToBottom;
    }

    // read preview size, grid width and grid height
    m_previewSize = config->readNumEntry("Preview Size", -1);
    m_gridWidth = config->readNumEntry("Grid Width", -1);
    m_gridHeight = config->readNumEntry("Grid Height", -1);
    m_gridSpacing = config->readNumEntry("Grid Spacing", -1);

    if (mode == DolphinIconsView::Previews) {
        if (m_previewSize < 0) {
            m_previewSize = TDEIcon::SizeEnormous;
        }
        if (m_gridWidth < 0) {
            m_gridWidth = m_previewSize + (m_previewSize / 2);
        }
    }
    else if (m_gridWidth < 0) {
        m_gridWidth = m_iconSize + (m_iconSize / 2) + (TDEIcon::SizeLarge * 2);
    }

    if (m_gridHeight < 0) {
        m_gridHeight = m_iconSize * 2;
    }

    // read font size and font family
    m_fontSize = config->readNumEntry("Font Size", -1);
    m_fontFamily = config->readEntry("Font Family");

    const TQFont font(TDEGlobalSettings::generalFont());
    if (m_fontSize < 0) {
        m_fontSize = font.pointSize();
    }

    if (m_fontFamily.isEmpty()) {
        m_fontFamily = font.family();
    }

    // read textlines count
    m_textlinesCount = config->readNumEntry("Number of Textlines", 2);
}


DolphinIconsViewSettings::~DolphinIconsViewSettings()
{
}

void DolphinIconsViewSettings::setIconSize(int size)
{
    // TODO: add boundaries check
    m_iconSize = size;
}

void DolphinIconsViewSettings::setPreviewSize(int size)
{
    // TODO: add boundaries check
    m_previewSize = size;
}

void DolphinIconsViewSettings::setGridSpacing(int spacing)
{
    // TODO: add boundaries check
    m_gridSpacing = spacing;
}

void DolphinIconsViewSettings::save()
{
    TDEConfig* config = kapp->config();
    setConfigGroup(config);

    config->writeEntry("Icon Size", m_iconSize);
    if (m_arrangement == TQIconView::LeftToRight) {
        config->writeEntry("Arrangement", "Left to Right");
    }
    else {
        config->writeEntry("Arrangement", "Top to Bottom");
    }

    config->writeEntry("Preview Size", m_previewSize);
    config->writeEntry("Grid Width", m_gridWidth);
    config->writeEntry("Grid Height", m_gridHeight);
    config->writeEntry("Grid Spacing", m_gridSpacing);
    config->writeEntry("Font Size", m_fontSize);
    config->writeEntry("Font Family", m_fontFamily);
    config->writeEntry("Number of Textlines", m_textlinesCount);
}

void DolphinIconsViewSettings::calculateGridSize(int hint)
{
    const int maxSize = (m_previewSize > m_iconSize) ? m_previewSize : m_iconSize;
    if (m_arrangement == TQIconView::LeftToRight) {
        int widthUnit = maxSize + (maxSize / 2);
        if (widthUnit < TDEIcon::SizeLarge) {
            widthUnit = TDEIcon::SizeLarge;
        }
        //m_gridWidth = widthUnit + hint * TDEIcon::SizeLarge;
        m_gridWidth = widthUnit + hint * TDEIcon::SizeLarge;

        m_gridHeight = m_iconSize;
        if (m_gridHeight <= TDEIcon::SizeMedium) {
            m_gridHeight = m_gridHeight * 2;
        }
        else {
            m_gridHeight += maxSize / 2;
        }
    }
    else {
        assert(m_arrangement == TQIconView::TopToBottom);
        m_gridWidth = maxSize + (hint + 1) * (8 * m_fontSize);

        // The height-setting is ignored yet by KFileIconView if the TopToBottom
        // arrangement is active. Anyway write the setting to have a defined value.
        m_gridHeight = maxSize;
    }
}

int DolphinIconsViewSettings::textWidthHint() const
{
    const int maxSize = (m_previewSize > m_iconSize) ? m_previewSize : m_iconSize;
    int hint = 0;
    if (m_arrangement == TQIconView::LeftToRight) {
        int widthUnit = maxSize + (maxSize / 2);
        if (widthUnit < TDEIcon::SizeLarge) {
            widthUnit = TDEIcon::SizeLarge;
        }
        hint = (m_gridWidth - widthUnit) / TDEIcon::SizeLarge;
    }
    else {
        assert(m_arrangement == TQIconView::TopToBottom);
        hint = (m_gridWidth - maxSize) / (8 * m_fontSize) - 1;
        if (hint > 2) {
            hint = 2;
        }
    }
    return hint;
}

void DolphinIconsViewSettings::setConfigGroup(TDEConfig* config)
{
    if (m_layoutMode == DolphinIconsView::Previews) {
        config->setGroup("Previews Mode");
    }
    else {
        config->setGroup("Icons Mode");
    }
}