summaryrefslogtreecommitdiffstats
path: root/src/mainwindow.cpp
blob: c578c434592809bad480e49cc7e8d059ce23884a (plain) (blame)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2012-2015 Christian Surlykke, Petr Vanek
*
* This file is part of qt-lightdm-greeter 
* It is distributed under the LGPL 2.1 or later license.
* Please refer to the LICENSE file for a copy of the license.
*/
#include <QRect>
#include <QApplication>
#include <QDesktopWidget>
#include <QPalette>
#include <QString>
#include <QDebug>
#include <QTextEdit>
#include <QStyleFactory>
#include <QSvgWidget>

#include "mainwindow.h"
#include "loginform.h"
#include "settings.h"

static const Settings _settings;

MainWindow::MainWindow(bool primary, int screen, const QRect &screenRect, QWidget *parent) :
    QWidget(parent),
    m_Screen(screen),
    m_Primary(primary)
{
    setObjectName(QString("MainWindow_%1").arg(screen));
    
    setGeometry(screenRect);

    setBackground();

    // display login dialog only in the main screen

    int spaceY = screenRect.height() / 2;
    
    if (showLoginForm()) {
        m_LoginForm = new LoginForm(this);
        spaceY -= m_LoginForm->height() / 2;

        int maxX = screenRect.width() - m_LoginForm->width();
        int maxY = screenRect.height() - m_LoginForm->height();
        int defaultX = 50*maxX/100;
        int defaultY = 50*maxY/100;
        int offsetX = getOffset(_settings.offsetX(), maxX, defaultX);
        int offsetY = getOffset(_settings.offsetY(), maxY, defaultY);
        
        m_LoginForm->move(offsetX, offsetY);
        m_LoginForm->show();

        // This hack ensures that the primary screen will have focus
        // if there are more screens (move the mouse cursor in the center
        // of primary screen - not in the center of all X area). It
        // won't affect single-screen environments.
        int centerX = screenRect.width()/2 + screenRect.x();
        int centerY = screenRect.height()/2 + screenRect.y();
        QCursor::setPos(centerX, centerY);
    }

    // Message log at the bottom
    m_messages = new QTextEdit(this);
    int logHeight = screenRect.height() * 3 / 4;
    if (logHeight > spaceY - 10) {
        logHeight = spaceY - 10;
    }
    m_messages->move(0, screenRect.height() * 2 / 3);
    m_messages->setFixedSize(screenRect.width(), screenRect.height() / 3);
    QPalette p = m_messages->palette();
    p.setColor(QPalette::Base, QColor(0,0,0,0)); // r,g,b,A
    QPalette::Light;
    m_messages->setPalette(p);
    //m_messages->setText("TODO\nLogmessages\n\nWarnings\netc.");
    m_messages->setReadOnly(true);
    this->setStyle(QStyleFactory::create("#m_messages, QTextEdit { border: none; }")); // TODO: Get rid of black 1px border

    // Banner
    if (_settings.bannerImagePath().isEmpty()) {
        QSvgWidget *banner = new QSvgWidget(_settings.bannerImagePath(), this);
        if (banner->sizeHint().height() > 0) {
            int bw, bh;
            QSize sh = banner->sizeHint();
            if (sh.height() < spaceY) {
                banner->setFixedSize(sh);
                bw = sh.width();
                bh = sh.height();
            } else {
                int bh = spaceY - 20;
                int bw = sh.width() * bh / sh.height();
                banner->setFixedSize(bw, bh);
            }
            banner->move((screenRect.width() - bw) / 2, (spaceY - bh) / 2);
        }
    }
    // TODO: UniLogo
}

MainWindow::~MainWindow()
{
}

bool MainWindow::showLoginForm()
{
    return m_Primary;
}

void MainWindow::setFocus(Qt::FocusReason reason)
{
    if (m_LoginForm) {
        m_LoginForm->setFocus(reason);
    }
    else  {
        QWidget::setFocus(reason);
    }
}

int MainWindow::getOffset(QString settingsOffset, int maxVal, int defaultVal)
{

    int offset = defaultVal > maxVal ? maxVal : defaultVal;

    if (! settingsOffset.isEmpty())  {
        if (QRegExp("^\\d+px$", Qt::CaseInsensitive).exactMatch(settingsOffset))  {
            offset = settingsOffset.left(settingsOffset.size() - 2).toInt();
            if (offset > maxVal) offset = maxVal;
        }
        else if (QRegExp("^\\d+%$", Qt::CaseInsensitive).exactMatch(settingsOffset)) {
            int offsetPct = settingsOffset.left(settingsOffset.size() -1).toInt();
            if (offsetPct > 100) offsetPct = 100;
            offset = (maxVal * offsetPct)/100;
        }
        else {
            qWarning() << "Could not understand" << settingsOffset
                       << "- must be of form <positivenumber>px or <positivenumber>%, e.g. 35px or 25%" ;
        }
    }

    return offset;
}

void MainWindow::setBackground()
{
    Qt::AspectRatioMode arMode = Qt::KeepAspectRatioByExpanding;
    QString bgPath = _settings.backgrundImagePath();
    if (bgPath.length() != 0) {
        m_background = QImage(bgPath);
        if (m_background.isNull()) {
            qWarning() << "Not able to read" << bgPath << "as image";
        }
             
    }

    if (m_background.isNull()) {
        arMode = Qt::IgnoreAspectRatio;
        QStringList cols = _settings.gradientColors();
        if (cols.length() == 4 || cols.length() == 2) {
            bool ok = true;
            uint a, b, c, d;
            if (ok) c = a = cols.at(0).toUInt(&ok, 16) | 0xff000000;
            if (ok) d = b = cols.at(1).toUInt(&ok, 16) | 0xff000000;
            if (cols.length() == 4) {
                if (ok) c = cols.at(2).toUInt(&ok, 16) | 0xff000000;
                if (ok) d = cols.at(3).toUInt(&ok, 16) | 0xff000000;
            }
            if (ok) {
                m_background = QImage(cols.length() / 2, 2, QImage::Format_RGB32);
                m_background.setPixel(0, 0, a);
                m_background.setPixel(0, 1, c);
                if (cols.length() == 4) {
                    m_background.setPixel(1, 0, b);
                    m_background.setPixel(1, 1, d);
                }
            }
        }
    }
    if (m_background.isNull()) {
        // Hard-coded default: Gradient
        m_background = QImage(2, 2, QImage::Format_RGB32);
        m_background.setPixel(0, 0, 0xffffffff);
        m_background.setPixel(1, 0, 0xff888687);
        m_background.setPixel(0, 1, 0xff888687);
        m_background.setPixel(1, 1, 0xfff9a72b);
    }
    
    QPalette palette;
    QRect rect = QApplication::desktop()->screenGeometry(m_Screen);
    if (m_background.isNull()) {
        palette.setColor(QPalette::Background, Qt::black);
    }
    else {
    	m_background = m_background.scaled(rect.width(), rect.height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    	int xoff = (m_background.width() -  rect.width()) / 2;
    	int yoff = (m_background.height() - rect.height()) / 2;
    	if (xoff != 0 || yoff != 0) {
    		m_background = m_background.copy(xoff, yoff, m_background.width(), m_background.height());
    	}
        QBrush brush(m_background);
        palette.setBrush(this->backgroundRole(), brush);
    }
    this->setPalette(palette);
}