summaryrefslogblamecommitdiffstats
path: root/src/mainwindow.cpp
blob: b66a73d9014ca952227e8c344c6a0279a0d26465 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                             
                  

                        
                 
                     

                                      
                    
 


                       


                       
                   
 
                                                                                            
                    

                             
                             

                        


                                                        



                            


                                                                                            
                                                   

                                         

                          
                                          
                                            


                                                               
                                   
                                   

                                                                     











                                                                         
 
             


                                                                               
                                         
                                            



                           
                                                                                                                   
                                                                                                    




                                                                                                          

         


                                                                   

                                                                       

                                                                                               
                          
                                                                                                                                                


                                        
                  





                         

                                              
                                                  




                                                 




                                                                                                      
                        
                       

 




                                                


                                                                                                              
                                                      


                                                                                                             
     


                                            
                                                                                                                             
                        
                       

 

                                                   
                                              
































                                                                                                  


                                           
                                                               


                          





                                                                                          


                                                
                           











                                   





                                            
                                  
                                                                                               



                                                       



                                          

                                
                     





































                                                                                                         
                                                                
                                                    

                                      
                                    
                                                                     
         
     

                                
                                                   
                                       
     
                                


                                                          
                                                

                                                
     
 
                     
                                


                                                          
                                                                                                                          

                                                                       


                                                                                                      
                                   




                                                        

                              
                                     









                                                                                                                                
                                            

                                   
                                                   



                                            















                                                                   
                          
                                                                    

                                                    
/*
* 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 <QtDebug>
#include <QTextEdit>
#include <QStyleFactory>
#include <QLabel>
#include <QSvgWidget>
#include <QSvgRenderer>
#include <QAbstractTextDocumentLayout>
#include <QDateTime>

#include <sys/types.h>
#include <sys/socket.h>

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

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

    setBackground();

    // TODO: Check if testMode == false and greeter == NULL, if so display big error message
    // instead of exiting/crashing

    // 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);
    }

    // Banner
    if (!Settings::bannerImagePath().isEmpty()) {
        qWarning() << "Have banner " << Settings::bannerImagePath();
        QSvgWidget *banner = new QSvgWidget(Settings::bannerImagePath(), this);
        qWarning() << banner->sizeHint();
        if (banner->renderer()->isValid()) {
            QSize sh;
            int offs = 0;
            do {
                offs += 20;
                sh = banner->sizeHint().scaled(screenRect.width() - offs, spaceY - offs - 50, Qt::KeepAspectRatio);
            } while (offs < 200 && sh.width() > screenRect.width() / 2 && sh.height() > spaceY / 2);
            int yoff = (spaceY - sh.height());
            if (yoff < 100) {
                yoff = 100;
            }
            banner->setGeometry((screenRect.width() - sh.width()) / 2, yoff / 2, sh.width(), sh.height());
        }
    }
    int ls = (spaceY > 500 ? 500 : spaceY);
    if (ls > screenRect.height() / 5) ls = screenRect.height() / 5;
    if (ls > screenRect.width() / 5) ls = screenRect.width() / 5;
    QRect logoRect(QPoint(0, screenRect.height() - ls), QSize(ls, ls));
    QSize logoSize = createLogo(logoRect);
    QRect distroRect(QPoint(screenRect.width() - ls, screenRect.height() - ls), QSize(ls, ls));
    QSize distroSize = createDistro(distroRect);
    if (showLoginForm()) {
        QRect lwSize(QPoint(logoSize.width(), screenRect.height() * 3/4), QPoint(screenRect.width() - distroSize.width(), screenRect.height()));
        lwSize.adjust(10, 10, -10, -10);
        createLogWindow(lwSize);
    }
    createClock();
}

MainWindow::~MainWindow()
{
}

QSize MainWindow::createLogo(const QRect &max)
{
    QString path = Settings::bottomLeftLogoPath();
    if (path.isEmpty())
        return QSize(0, 0);
    QSvgWidget *img = new QSvgWidget(path, this);
    if (!img->renderer()->isValid())
        return QSize(0, 0);
    QSize virtualSize = img->sizeHint().scaled(max.width(), max.height(), Qt::KeepAspectRatio);
    QSize realSize = img->sizeHint().scaled(max.width() - 20, max.height() - 20, Qt::KeepAspectRatio);
    if (virtualSize.width() == 0 || virtualSize.height() == 0)
        return QSize(0, 0);
    QRect c(10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height());
    img->setGeometry(c);
    return virtualSize;
}

QSize MainWindow::createDistro(const QRect &max)
{
    QImage img(QString("/etc/distro.png"));
    if (img.isNull())
        return QSize(0, 0);
    QSize realSize = img.size();
    QSize virtualSize = realSize.scaled(realSize.width() + 20, realSize.height() + 20, Qt::IgnoreAspectRatio);
    if (virtualSize.width() > max.width() || virtualSize.height() > max.height()) {
        // This requires that the given rect is square
        img = img.scaled(max.width() - 20, max.height() - 20, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        realSize = img.size();
        virtualSize = realSize.scaled(realSize.width() + 20, realSize.height() + 20, Qt::IgnoreAspectRatio);
    }
    QPixmap pixmap(QPixmap::fromImage(img));
    QLabel *lbl = new QLabel(this);
    lbl->setPixmap(pixmap);
    QRect c(max.right() - realSize.width() - 10, max.bottom() - realSize.height() - 10, realSize.width(), realSize.height());
    lbl->setGeometry(c);
    return virtualSize;
}

void MainWindow::createLogWindow(const QRect& geom)
{
    QString path = Settings::logMessageFile();
    if (path.isEmpty())
        return;
    QFile f(path);
    if (f.size() == 0 || !f.open(QFile::ReadOnly))
        return;
    m_messages = new QTextEdit(this);
    m_messages->setGeometry(geom);
    int ps = geom.height() / 20;
    if (ps > 20) ps = 20;
    m_messages->setFontPointSize(ps);
    QTextStream stream(&f);
    const QColor black(Qt::black);
    while (!stream.atEnd()) {
        bool ok = false;
        QString line(stream.readLine());
        int i = line.indexOf(' ');
        if (i > 0) {
            QString scol(line.left(i));
            uint col = scol.toUInt(&ok, 16);
            if (ok) {
                m_messages->setTextColor(QColor(QRgb(col)));
                line = line.mid(i + 1);
            }
        }
        if (!ok) {
            m_messages->setTextColor(black);
        }
        m_messages->append(line);
    }
    m_messages->setReadOnly(true);
    m_messages->setStyleSheet("border:none; background:rgba(255,255,255,.33); border-radius:5px");
}

void MainWindow::createClock()
{
    QString style = Settings::clockStyle();
    QString backgroundStyle = Settings::clockBackgroundStyle();
    if (style == "none")
        return;
    if (style.isEmpty()) {
        style = "border:none; color:#fff; font-size:14pt; qproperty-alignment:AlignRight";
    }
    if (backgroundStyle.isEmpty()) {
        backgroundStyle = "#bg { border:none; background:#888687 }";
    } else {
        backgroundStyle = "#bg { " + backgroundStyle + " }";
    }
    QStringList slist = Settings::clockShadow();
    int x = 1, y = 1, blur = 1;
    QString color = "#555";
    if (slist.length() > 0) {
        x = slist.at(0).toInt();
    }
    if (slist.length() > 1) {
        y = slist.at(1).toInt();
    }
    if (slist.length() > 2) {
        color = slist.at(2);
    }
    if (slist.length() > 3) {
        blur = slist.at(3).toInt();
    }
    QWidget *bg = new QWidget(this);
    bg->setObjectName("bg");
    bg->setFixedWidth(this->width());
    bg->setMaximumHeight(500);
    bg->setStyleSheet(backgroundStyle);
    m_Clock = new QLabel("Today 00:00", bg);
    m_Clock->setStyleSheet(style);
    QGraphicsDropShadowEffect *pLabelTextShadowEffect = new QGraphicsDropShadowEffect(m_Clock);
    pLabelTextShadowEffect->setColor(QColor(color));
    pLabelTextShadowEffect->setBlurRadius(blur);
    pLabelTextShadowEffect->setOffset(x, y);
    m_Clock->setGraphicsEffect(pLabelTextShadowEffect);
    m_Clock->setFixedWidth(this->width());
    updateClock();
}

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()) {
        m_background = Global::getConfigGradient();
        arMode = Qt::IgnoreAspectRatio;
    }
    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, 0xffffffff);
        m_background.setPixel(0, 1, 0xff888687);
        m_background.setPixel(1, 1, 0xfff9a72b);
    }

    QPalette palette;
    if (m_background.isNull()) {
        palette.setColor(QPalette::Background, Qt::black);
    }
    else {
        m_background = m_background.scaled(m_ScreenRect.width(), m_ScreenRect.height(), arMode, Qt::SmoothTransformation);
        int xoff = (m_background.width() -  m_ScreenRect.width()) / 2;
        int yoff = (m_background.height() - m_ScreenRect.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);
}

void MainWindow::showStandby()
{
	if (m_LoginForm != nullptr) {
		m_LoginForm->hide();
	}
	QSvgWidget *img = new QSvgWidget(":/resources/gnome-face-tired.svg", this);
	if (img->renderer()->isValid()) {
		QSize sh = img->sizeHint().scaled(this->width() / 2, this->height() / 2, Qt::KeepAspectRatio);
		img->setGeometry((this->width() - sh.width()) / 2, (this->height() - sh.height()) / 2, sh.width(), sh.height());
		img->show();
	} else {
		qWarning() << "Shice!";
	}
    QTimer::singleShot(4000, [this, img]() {
		img->hide();
		img->deleteLater();
		if (this->m_LoginForm != nullptr) {
			m_LoginForm->show();
		}
	});
}

void MainWindow::updateClock()
{
    int next = drawClock();
    if (next > 0) {
        QTimer::singleShot(next, this, SLOT(updateClock()));
    }
}

int MainWindow::drawClock()
{
    if (m_Clock == nullptr)
        return 0;
    QDateTime time = QDateTime::currentDateTime();
    QLocale loc;
    m_Clock->setText(time.toString(loc.dateFormat() + "  HH:mm "));
    m_Clock->adjustSize();
	reinterpret_cast<QWidget*>(m_Clock->parent())->adjustSize();
    return (60 - time.time().second()) * 1000 + 100;
}