File: LayoutSettingsHandler.cpp

package info (click to toggle)
subcommander 2.0.0~b5p1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 14,880 kB
  • ctags: 9,003
  • sloc: cpp: 63,591; sh: 3,904; xml: 1,984; makefile: 1,166; ansic: 786; ruby: 251; lisp: 24
file content (157 lines) | stat: -rw-r--r-- 4,322 bytes parent folder | download | duplicates (4)
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
/* ====================================================================
 * Copyright (c) 2007-2008  Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "LayoutSettingsHandler.h"
#include "../config/ConfigData.h"

// qt
#include <QtCore/QRegExp>
#include <Qt3Support/Q3Header>


int LayoutSettingsHandler::getValue( ConfigData* cfg, const QString& key, int def )
{
  ConfigValue value = cfg->getValue( sc::String(key.toUtf8()) );
  
  if( value.isNull() )
  {
    return def;
  }
  
  return value.getNumericValue();
}

void LayoutSettingsHandler::setValue( ConfigData* cfg, const QString& key, int value )
{
  ConfigValue val( sc::String(key.toUtf8()), (long)value);
  cfg->setValue( val.getKey(), val );
}

QSize LayoutSettingsHandler::getSize( ConfigData* cfg, const QString& key, const QSize& def )
{
  ConfigValue sizeValue = cfg->getValue( sc::String(key.toUtf8()) );
  
  if( sizeValue.isNull() )
  {
    return def;
  }

  QString sizeVal = (const char*)sizeValue.getStringValue();
  QRegExp sizeExp("(\\d+),(\\d+)");
  sizeExp.exactMatch(sizeVal);

  return QSize( sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt() );
}

void LayoutSettingsHandler::setSize( ConfigData* cfg, const QString& key, const QSize& size )
{
  QString sizeVal = QString("%1,%2").arg(size.width()).arg(size.height());
  
  ConfigValue value( sc::String(key.toUtf8()), sc::String(sizeVal.toUtf8()) );
  cfg->setValue( value.getKey(), value );
}

QRect LayoutSettingsHandler::getRect( ConfigData* cfg, const QString& key, const QRect& def )
{
  ConfigValue rectValue = cfg->getValue( sc::String(key.toUtf8()) );
  
  if( rectValue.isNull() )
  {
    return def;
  }
  
  QString rectVal = (const char*)rectValue.getStringValue();
  QRegExp rectExp("(\\d+),(\\d+),(\\d+),(\\d+)");
  rectExp.exactMatch(rectVal);

  QRect rect;
  rect.setLeft( rectExp.cap(1).toInt() );
  rect.setTop( rectExp.cap(2).toInt() );
  rect.setRight( rectExp.cap(3).toInt() );
  rect.setBottom( rectExp.cap(4).toInt() );
  return rect;
}

void LayoutSettingsHandler::setRect( ConfigData* cfg, const QString& key, const QRect& rect )
{
  QString rectVal  = QString("%1,%2,%3,%4")
    .arg(rect.left()).arg(rect.top()).arg(rect.right()).arg(rect.bottom());
  
  ConfigValue value( sc::String(key.toUtf8()), sc::String(rectVal.toUtf8()) );
  cfg->setValue( value.getKey(), value );
}

QByteArray LayoutSettingsHandler::getByteArray( ConfigData* cfg, const QString& key, const QByteArray& def )
{
  ConfigValue bytearrayValue = cfg->getValue( sc::String(key.toUtf8()) );
  
  if( bytearrayValue.isNull() )
  {
    return def;
  }

  return QByteArray::fromHex((const char*)bytearrayValue.getStringValue());
}

void LayoutSettingsHandler::setByteArray( ConfigData* cfg, const QString& key, const QByteArray& bytearray )
{
  ConfigValue value( sc::String(key.toUtf8()), sc::String(bytearray.toHex()) );
  cfg->setValue( value.getKey(), value );
}

void LayoutSettingsHandler::getHeaderColumns( ConfigData* cfg, const QString& key, Q3Header* header )
{
  ConfigValue value = cfg->getValue( sc::String(key.toUtf8()) );

  if( value.isNull() )
  {
    return;
  }
  
  QString qval = (const char*)value.getStringValue();

  int cnt = 0;
  while(true)
  {    
    QString section = qval.section( ',', cnt );

    if( section.isEmpty() )
      break;

    QRegExp exp("(\\d+):(\\d+)");
    exp.exactMatch(section);
    
    header->moveSection( exp.cap(1).toInt(), cnt );
    header->resizeSection( exp.cap(1).toInt(), exp.cap(2).toInt() );
    cnt++;
  }
}

void LayoutSettingsHandler::setHeaderColumns( ConfigData* cfg, const QString& key, const Q3Header* header )
{
  QString sections;
  
  for( int i = 0; i < header->count(); i++ )
  {
    QString v;
    int s = header->mapToSection(i);
    v.setNum(s);
    sections += v;
    sections += ":";    
    v.setNum( header->sectionSize(s) );
    sections += v;
    sections += ",";    
  }
  // strip trailing ","
  sections.truncate( sections.length()-1 );

  ConfigValue value( sc::String(key.toUtf8()), sc::String(sections.toUtf8()) );
  cfg->setValue( value.getKey(), value );
}