xref: /glogg/src/configuration.cpp (revision bb02e0acf44ddb4e4f83d6127a1e488789162922)
1 /*
2  * Copyright (C) 2009, 2010, 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <QFontInfo>
21 
22 #include "log.h"
23 
24 #include "configuration.h"
25 
26 Configuration::Configuration()
27 {
28     // Should have some sensible default values.
29     mainFont_ = QFont("mono", 10);
30     mainFont_.setStyleHint( QFont::Courier, QFont::PreferOutline );
31 
32     mainRegexpType_               = ExtendedRegexp;
33     quickfindRegexpType_          = FixedString;
34     quickfindIncremental_         = true;
35 
36     overviewVisible_              = true;
37     lineNumbersVisibleInMain_     = false;
38     lineNumbersVisibleInFiltered_ = true;
39 
40     QFontInfo fi(mainFont_);
41     LOG(logDEBUG) << "Default font is " << fi.family().toStdString();
42 }
43 
44 // Accessor functions
45 QFont Configuration::mainFont() const
46 {
47     return mainFont_;
48 }
49 
50 void Configuration::setMainFont( QFont newFont )
51 {
52     LOG(logDEBUG) << "Configuration::setMainFont";
53 
54     mainFont_ = newFont;
55 }
56 
57 void Configuration::retrieveFromStorage( QSettings& settings )
58 {
59     LOG(logDEBUG) << "Configuration::retrieveFromStorage";
60 
61     // Fonts
62     QString family = settings.value( "mainFont.family" ).toString();
63     int size = settings.value( "mainFont.size" ).toInt();
64 
65     // If no config read, keep the default
66     if ( !family.isNull() )
67         mainFont_ = QFont( family, size );
68 
69     // Regexp types
70     mainRegexpType_ = static_cast<SearchRegexpType>(
71             settings.value( "regexpType.main", mainRegexpType_ ).toInt() );
72     quickfindRegexpType_ = static_cast<SearchRegexpType>(
73             settings.value( "regexpType.quickfind", quickfindRegexpType_ ).toInt() );
74     if ( settings.contains( "quickfind.incremental" ) )
75         quickfindIncremental_ = settings.value( "quickfind.incremental" ).toBool();
76 
77     // View settings
78     if ( settings.contains( "view.overviewVisible" ) )
79         overviewVisible_ = settings.value( "view.overviewVisible" ).toBool();
80     if ( settings.contains( "view.lineNumbersVisibleInMain" ) )
81         lineNumbersVisibleInMain_ =
82             settings.value( "view.lineNumbersVisibleInMain" ).toBool();
83     if ( settings.contains( "view.lineNumbersVisibleInFiltered" ) )
84         lineNumbersVisibleInFiltered_ =
85             settings.value( "view.lineNumbersVisibleInFiltered" ).toBool();
86 
87     // Some sanity check (mainly for people upgrading)
88     if ( quickfindIncremental_ )
89         quickfindRegexpType_ = FixedString;
90 }
91 
92 void Configuration::saveToStorage( QSettings& settings ) const
93 {
94     LOG(logDEBUG) << "Configuration::saveToStorage";
95 
96     QFontInfo fi(mainFont_);
97 
98     settings.setValue( "mainFont.family", fi.family() );
99     settings.setValue( "mainFont.size", fi.pointSize() );
100     settings.setValue( "regexpType.main", static_cast<int>( mainRegexpType_ ) );
101     settings.setValue( "regexpType.quickfind", static_cast<int>( quickfindRegexpType_ ) );
102     settings.setValue( "quickfind.incremental", quickfindIncremental_ );
103     settings.setValue( "view.overviewVisible", overviewVisible_ );
104     settings.setValue( "view.lineNumbersVisibleInMain", lineNumbersVisibleInMain_ );
105     settings.setValue( "view.lineNumbersVisibleInFiltered", lineNumbersVisibleInFiltered_ );
106 }
107