1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Ivan Komissarov
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** BSD License Usage
19** Alternatively, you may use this file under the terms of the BSD license
20** as follows:
21**
22** "Redistribution and use in source and binary forms, with or without
23** modification, are permitted provided that the following conditions are
24** met:
25** * Redistributions of source code must retain the above copyright
26** notice, this list of conditions and the following disclaimer.
27** * Redistributions in binary form must reproduce the above copyright
28** notice, this list of conditions and the following disclaimer in
29** the documentation and/or other materials provided with the
30** distribution.
31** * Neither the name of The Qt Company Ltd nor the names of its
32** contributors may be used to endorse or promote products derived
33** from this software without specific prior written permission.
34**
35**
36** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
47**
48** $QT_END_LICENSE$
49**
50****************************************************************************/
51
52#include "storagemodel.h"
53
54#include <QDir>
55#include <QLocale>
56
57void StorageModel::refresh()
58{
59 beginResetModel();
60 m_volumes = QStorageInfo::mountedVolumes();
61 std::sort(first: m_volumes.begin(), last: m_volumes.end(),
62 comp: [](const QStorageInfo &st1, const QStorageInfo &st2) {
63 static const QString rootSortString = QStringLiteral(" ");
64 return (st1.isRoot() ? rootSortString : st1.rootPath())
65 < (st2.isRoot() ? rootSortString : st2.rootPath());
66 });
67 endResetModel();
68}
69
70int StorageModel::columnCount(const QModelIndex &/*parent*/) const
71{
72 return ColumnCount;
73}
74
75int StorageModel::rowCount(const QModelIndex &parent) const
76{
77 if (parent.isValid())
78 return 0;
79 return m_volumes.count();
80}
81
82Qt::ItemFlags StorageModel::flags(const QModelIndex &index) const
83{
84 Qt::ItemFlags result = QAbstractTableModel::flags(index);
85 switch (index.column()) {
86 case ColumnAvailable:
87 case ColumnIsReady:
88 case ColumnIsReadOnly:
89 case ColumnIsValid:
90 result |= Qt::ItemIsUserCheckable;
91 break;
92 default:
93 break;
94 }
95 return result;
96}
97
98QVariant StorageModel::data(const QModelIndex &index, int role) const
99{
100 if (!index.isValid())
101 return QVariant();
102
103 if (role == Qt::DisplayRole) {
104 const QStorageInfo &volume = m_volumes.at(i: index.row());
105 switch (index.column()) {
106 case ColumnRootPath:
107 return QDir::toNativeSeparators(pathName: volume.rootPath());
108 case ColumnName:
109 return volume.name();
110 case ColumnDevice:
111 return volume.device();
112 case ColumnFileSystemName:
113 return volume.fileSystemType();
114 case ColumnTotal:
115 return QLocale().formattedDataSize(bytes: volume.bytesTotal());
116 case ColumnFree:
117 return QLocale().formattedDataSize(bytes: volume.bytesFree());
118 case ColumnAvailable:
119 return QLocale().formattedDataSize(bytes: volume.bytesAvailable());
120 default:
121 break;
122 }
123 } else if (role == Qt::CheckStateRole) {
124 const QStorageInfo &volume = m_volumes.at(i: index.row());
125 switch (index.column()) {
126 case ColumnIsReady:
127 return volume.isReady();
128 case ColumnIsReadOnly:
129 return volume.isReadOnly();
130 case ColumnIsValid:
131 return volume.isValid();
132 default:
133 break;
134 }
135 } else if (role == Qt::TextAlignmentRole) {
136 switch (index.column()) {
137 case ColumnTotal:
138 case ColumnFree:
139 case ColumnAvailable:
140 return Qt::AlignTrailing;
141 default:
142 break;
143 }
144 return Qt::AlignLeading;
145 } else if (role == Qt::ToolTipRole) {
146 QLocale locale;
147 const QStorageInfo &volume = m_volumes.at(i: index.row());
148 return tr(s: "Root path : %1\n"
149 "Name: %2\n"
150 "Display Name: %3\n"
151 "Device: %4\n"
152 "FileSystem: %5\n"
153 "Total size: %6\n"
154 "Free size: %7\n"
155 "Available size: %8\n"
156 "Is Ready: %9\n"
157 "Is Read-only: %10\n"
158 "Is Valid: %11\n"
159 "Is Root: %12"
160 ).
161 arg(a: QDir::toNativeSeparators(pathName: volume.rootPath())).
162 arg(a: volume.name()).
163 arg(a: volume.displayName()).
164 arg(a: QString::fromUtf8(str: volume.device())).
165 arg(a: QString::fromUtf8(str: volume.fileSystemType())).
166 arg(a: locale.formattedDataSize(bytes: volume.bytesTotal())).
167 arg(a: locale.formattedDataSize(bytes: volume.bytesFree())).
168 arg(a: locale.formattedDataSize(bytes: volume.bytesAvailable())).
169 arg(a: volume.isReady() ? tr(s: "true") : tr(s: "false")).
170 arg(a: volume.isReadOnly() ? tr(s: "true") : tr(s: "false")).
171 arg(a: volume.isValid() ? tr(s: "true") : tr(s: "false")).
172 arg(a: volume.isRoot() ? tr(s: "true") : tr(s: "false"));
173 }
174 return QVariant();
175}
176
177QVariant StorageModel::headerData(int section, Qt::Orientation orientation, int role) const
178{
179 if (orientation != Qt::Horizontal)
180 return QVariant();
181
182 if (role != Qt::DisplayRole)
183 return QVariant();
184
185 switch (section) {
186 case ColumnRootPath:
187 return tr(s: "Root Path");
188 case ColumnName:
189 return tr(s: "Volume Name");
190 case ColumnDevice:
191 return tr(s: "Device");
192 case ColumnFileSystemName:
193 return tr(s: "File System");
194 case ColumnTotal:
195 return tr(s: "Total");
196 case ColumnFree:
197 return tr(s: "Free");
198 case ColumnAvailable:
199 return tr(s: "Available");
200 case ColumnIsReady:
201 return tr(s: "Ready");
202 case ColumnIsReadOnly:
203 return tr(s: "Read-only");
204 case ColumnIsValid:
205 return tr(s: "Valid");
206 default:
207 break;
208 }
209
210 return QVariant();
211}
212

source code of qtbase/examples/widgets/itemviews/storageview/storagemodel.cpp