File: OptionModel.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (158 lines) | stat: -rw-r--r-- 5,063 bytes parent folder | download | duplicates (3)
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
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "BaseIconNames.h"
#include "IconEngine.h"
#include "OptionModel.h"
#include "XmlOptions.h"

#include <QObject>
#include <QPalette>

//_______________________________________________
namespace Base
{
    template<> bool isChild<OptionPair>( const OptionPair& first, const OptionPair& second )
    { return second.first == first.first && second.second.raw().isEmpty(); }
};

//_______________________________________________
Qt::ItemFlags OptionModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) return 0;
    else if( !get( index ).second.hasFlag( Option::Flag::Recordable ) ) return Qt::ItemIsSelectable;
    else if( !( isReadOnly() || rowCount( index ) ) && index.column() == Value ) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    else return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

//__________________________________________________________________
QVariant OptionModel::data( const QModelIndex& index, int role ) const
{

    // check index, role and column
    if( !index.isValid() ) return QVariant();

    const OptionPair& option( get( index ) );

    // return text associated to file and column
    if( role == Qt::DisplayRole )
    {
        switch( index.column() )
        {
            case Name: return option.first;
            case Value: return option.second.raw();
            case DefaultValue: return option.second.defaultValue();
            case Flags: return static_cast<int>(option.second.flags());
            default: return QVariant();
        }
    }

    if( role == Qt::DecorationRole && index.column() == Current )
    { return option.second.isCurrent() ? IconEngine::get( IconNames::DialogAccept ):QVariant(); }

    return QVariant();

}

//__________________________________________________________________
bool OptionModel::setData(const QModelIndex &index, const QVariant& value, int role )
{

    if( !(index.isValid() && index.column() == Value && role == Qt::EditRole ) ) return false;

    // retrieve option
    const OptionPair source( get( index ) );
    const QString newString( value.toString() );
    if( !( newString.isNull() || newString == source.second.raw() ) )
    {

        OptionPair destination( source );
        destination.second.setRaw( newString );
        replace( source, destination );

        emit dataChanged( index, index );

        if( XmlOptions::get().isSpecialOption( source.first ) ) emit specialOptionModified( destination );
        else emit optionModified( destination );

    }

    return true;

}

//__________________________________________________________________
QVariant OptionModel::headerData(int section, Qt::Orientation orientation, int role) const
{

    if(
        orientation == Qt::Horizontal &&
        role == Qt::DisplayRole &&
        section >= 0 &&
        section < nColumns )
    { return columnTitles_[section]; }

    // return empty
    return QVariant();

}

//________________________________________________________
bool OptionModel::SortFTor::operator () ( OptionPair first, OptionPair second ) const
{

    if( order_ == Qt::DescendingOrder ) std::swap( first, second );

    switch( type_ )
    {

        case Name:
        {
            if( first.first != second.first ) return first.first < second.first;
            else return first.second.raw() < second.second.raw();
        }

        case Current:
        {
            if( first.second.isCurrent() != second.second.isCurrent() ) return first.second.isCurrent() < second.second.isCurrent();
            else return false;
        }

        case Value:
        {
            if( first.second.raw() != second.second.raw() ) return first.second.raw() < second.second.raw();
            else return false;
        }

        case DefaultValue:
        {
            if( first.second.defaultValue() != second.second.defaultValue() ) return first.second.defaultValue() < second.second.defaultValue();
            else return false;
        }

        case Flags:
        {
            if( first.second.flags() != second.second.flags() ) return first.second.flags() < second.second.flags();
            else return false;
        }

        default: return true;
    }

}