summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/fetchmore/window.cpp
blob: ffb89ce7df1bd65e5e1bc0b3d545da5f0f032a9a (plain)
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "window.h"
#include "filelistmodel.h"

#include <QtWidgets>

Window::Window(QWidget *parent)
    : QWidget(parent)
{
    model = new FileListModel(this);
    model->setDirPath(QDir::rootPath());

    view = new QListView;
    view->setModel(model);

    logViewer = new QPlainTextEdit(this);
    logViewer->setReadOnly(true);
    logViewer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
                                         QSizePolicy::Preferred));

    connect(model, &FileListModel::numberPopulated,
            this, &Window::updateLog);
    connect(view, &QAbstractItemView::activated,
            this, &Window::activated);

    auto *layout = new QVBoxLayout(this);
    layout->addWidget(view);
    layout->addWidget(logViewer);

    setWindowTitle(tr("Fetch More Example"));
}

void Window::updateLog(const QString &path, int start, int number, int total)
{
    const int last = start + number - 1;
    const QString nativePath = QDir::toNativeSeparators(path);
    const QString message = tr("%1..%2/%3 items from \"%4\" added.")
                            .arg(start).arg(last).arg(total).arg(nativePath);
    logViewer->appendPlainText(message);
}

void Window::activated(const QModelIndex &index)
{
    const QFileInfo fi = model->fileInfoAt(index);
    if (fi.isDir()) {
        logViewer->clear();
        model->setDirPath(fi.absoluteFilePath());
    }
}