1 | /* This file is part of the Kate project. |
2 | * |
3 | * Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public License |
16 | * along with this library; see the file COPYING.LIB. If not, write to |
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "kateprojectitem.h" |
22 | |
23 | #include <QDir> |
24 | #include <QFile> |
25 | #include <QFileInfo> |
26 | #include <QThread> |
27 | #include <QCoreApplication> |
28 | |
29 | #include <KMimeType> |
30 | #include <KIconLoader> |
31 | #include <KTextEditor/Document> |
32 | #include <KIcon> |
33 | |
34 | KateProjectItem::KateProjectItem (Type type, const QString &text) |
35 | : QStandardItem (text) |
36 | , m_type (type) |
37 | , m_icon (0) |
38 | , m_emblem (0) |
39 | { |
40 | } |
41 | |
42 | KateProjectItem::~KateProjectItem () |
43 | { |
44 | /** |
45 | * cleanup |
46 | */ |
47 | delete m_icon; |
48 | delete m_emblem; |
49 | } |
50 | |
51 | void KateProjectItem::slotModifiedChanged(KTextEditor::Document *doc) { |
52 | if (m_icon) { |
53 | delete m_icon; |
54 | m_icon=0; |
55 | } |
56 | if (doc->isModified()) { |
57 | if (m_emblem) { |
58 | QStringList emblems; |
59 | emblems<<*m_emblem; |
60 | m_icon=new KIcon("document-save" ,0,emblems); |
61 | } else |
62 | m_icon=new KIcon("document-save" ,0); |
63 | } |
64 | emitDataChanged(); |
65 | } |
66 | |
67 | void KateProjectItem::slotModifiedOnDisk (KTextEditor::Document *document, |
68 | bool isModified, KTextEditor::ModificationInterface::ModifiedOnDiskReason reason) { |
69 | Q_UNUSED(document) |
70 | Q_UNUSED(isModified) |
71 | |
72 | if (m_icon) { |
73 | delete m_icon; |
74 | m_icon=0; |
75 | } |
76 | |
77 | if (m_emblem) { |
78 | delete m_emblem; |
79 | m_emblem=0; |
80 | } |
81 | |
82 | if (reason!=KTextEditor::ModificationInterface::OnDiskUnmodified) |
83 | m_emblem=new QString("emblem-important" ); |
84 | emitDataChanged(); |
85 | |
86 | } |
87 | |
88 | QVariant KateProjectItem::data (int role) const |
89 | { |
90 | /** |
91 | * create icons on demand |
92 | */ |
93 | if (role == Qt::DecorationRole) { |
94 | /** |
95 | * this should only happen in main thread |
96 | * the background thread should only construct this elements and fill data |
97 | * but never query gui stuff! |
98 | */ |
99 | Q_ASSERT (QThread::currentThread () == QCoreApplication::instance()->thread ()); |
100 | |
101 | /** |
102 | * create icon, on demand |
103 | */ |
104 | if (!m_icon) { |
105 | /** |
106 | * use right type |
107 | */ |
108 | switch (m_type) { |
109 | case Project: |
110 | m_icon = new QIcon (KIconLoader::global ()->loadIcon ("folder-documents" , KIconLoader::Small)); |
111 | break; |
112 | |
113 | case Directory: |
114 | m_icon = new QIcon (KIconLoader::global ()->loadIcon ("folder" , KIconLoader::Small)); |
115 | break; |
116 | |
117 | case File: { |
118 | QString iconName = KMimeType::iconNameForUrl(KUrl::fromPath(data(Qt::UserRole).toString())); |
119 | QStringList emblems; |
120 | if (m_emblem) { |
121 | emblems<<*m_emblem; |
122 | } |
123 | kDebug( 13035 ) << emblems; |
124 | m_icon = new QIcon (KIconLoader::global ()->loadMimeTypeIcon (iconName, KIconLoader::Small,0,KIconLoader::DefaultState,emblems)); |
125 | break; |
126 | } |
127 | } |
128 | } |
129 | |
130 | /** |
131 | * return the cached icon |
132 | */ |
133 | return QVariant (*m_icon); |
134 | } |
135 | |
136 | /** |
137 | * use normal data method |
138 | */ |
139 | return QStandardItem::data (role); |
140 | } |
141 | |
142 | // kate: space-indent on; indent-width 2; replace-tabs on; |
143 | |