summaryrefslogtreecommitdiffstats
path: root/src/b2qt-flashing-wizard/platform_page.cpp
blob: 3adf9b990085f659bbfd22ecc860a363f383b779 (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use the contact form at
** http://qt.digia.com/
**
** This file is part of Qt Enterprise Embedded.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** the contact form at http://qt.digia.com/
**
****************************************************************************/

#include "platform_page.h"
#include <QRadioButton>
#include <QLayout>
#include <QDebug>
#include <QDir>
#include <QLabel>
#include <QSpacerItem>
#include "mainwindow.h" // Page_ enum

extern QString G_platform;
extern QString G_version;
extern QString G_os;
extern QString G_device;
extern QString G_board;
extern QString G_SDKDIR;

QLabel *createErrorLabel(QWidget *parent)
{
    QLabel *label = new QLabel(parent);
    label->setAlignment(Qt::AlignHCenter);
    label->setWordWrap(true);

    QFont f = label->font();
    f.setBold(true);
    label->setFont(f);

    QPalette p = label->palette();
    p.setColor(QPalette::WindowText, Qt::red);
    label->setPalette(p);

    return label;
}

PlatformPage::PlatformPage(QWidget *parent)
    : QWizardPage(parent)
    , mError(createErrorLabel(this))
    , mLayout(new QVBoxLayout(this))
{
    setTitle("Platform");
    setSubTitle("Select a platform to create a disk for");
    mLayout->addSpacerItem(new QSpacerItem(40,40,QSizePolicy::Minimum, QSizePolicy::Expanding));
    mLayout->addWidget(mError);
    setLayout(mLayout);
}

PlatformPage::~PlatformPage()
{
}

bool PlatformPage::isComplete() const
{
    QStringList data = buttonData();
    if (data.isEmpty())
        return false;

    if (data[0] == "nexus7") {
       mError->setText("The selected platform is currently not supported.");
       return false;
    }
    if (data[0] == "iMX6" && data[1] == "eAndroid") {
       mError->setText("The selected platform is currently not supported.");
       return false;
    }

    return !data.isEmpty();
}

void PlatformPage::itemSelected()
{
    mError->clear();
    emit completeChanged();
}

void PlatformPage::initializePage()
{
    mError->clear();

    qDeleteAll(mButtons);
    mButtons.clear();
    mButtonData.clear();

    QDir dir(G_SDKDIR);
    foreach (const QString i, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
        if (!i.startsWith("Boot2Qt-"))
          continue;

        QDir dir2(dir.absoluteFilePath(i));
        foreach (const QString j, dir2.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
            if (j.startsWith("emulator-"))
                continue;
            QStringList token = j.split('-');
            QString os = token.takeLast();
            QString name = token.join("-");

            if (os == "eAndroid" && name.startsWith("generic-")) {
                QString version = token[1];
                QDir dir3(dir2.absoluteFilePath(j) + "/images");
                foreach (const QString k, dir3.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
                    if (k == "common")
                        continue;

                    if (!QFile::exists(dir3.absoluteFilePath(k) + "/deploy.sh"))
                        continue;

                    QRadioButton *button = new QRadioButton;
                    button->setText(k + "-" + version + "-" + os + " (" + i + ")");
                    mLayout->insertWidget(0, button);
                    connect(button, &QRadioButton::toggled, this, &PlatformPage::itemSelected);
                    mButtons.append(button);
                    mButtonData.insert(button, QStringList() << name << os << i << k);
                }

            } else if (os == "eAndroid" || os == "eLinux") {
                QRadioButton *button = new QRadioButton;
                button->setText(j + " (" + i + ")");
                mLayout->insertWidget(0, button);
                connect(button, &QRadioButton::toggled, this, &PlatformPage::itemSelected);
                mButtons.append(button);
                mButtonData.insert(button, QStringList() << name << os << i << name);
            }
        }
    }
    if (mButtons.isEmpty()) {
        mError->setText("No suitable platform found.\nMake sure you have installed at least one hardware platform.");
    }
}

QStringList PlatformPage::buttonData() const
{
    QStringList data;

    foreach (QRadioButton *button, mButtons) {
        if (button->isChecked()) {
            data = mButtonData[button];
            break;
        }
    }

    return data;
}

bool PlatformPage::validatePage()
{
    QStringList data = buttonData();

    G_platform = data[0];
    G_os = data[1];
    G_version = data[2];
    G_board = data[3];

    qDebug() << "Selected:" << G_platform << G_os << G_version << G_board;
    return true;
}

int PlatformPage::nextId() const
{
    if (G_board.startsWith("nexus7"))
        return MainWindow::Page_Device;
    else
        return MainWindow::Page_Disk;
}