209 changes: 209 additions & 0 deletions src/app/qgsmessagebar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/***************************************************************************
qgsmessagebar.cpp - description
-------------------
begin : June 2012
copyright : (C) 2012 by Giuseppe Sucameli
email : sucameli at faunalia dot it
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmessagebar.h"
#include "qgsapplication.h"

#include <QWidget>
#include <QPalette>
#include <QStackedWidget>
#include <QLabel>
#include <QToolButton>
#include <QGridLayout>


QgsMessageBar::QgsMessageBar( QWidget *parent )
: QFrame( parent ), mCurrentItem ( NULL )
{
QPalette pal = palette();
pal.setBrush( backgroundRole(), pal.window() );
setPalette( pal );
setAutoFillBackground( true );
setFrameShape( QFrame::StyledPanel );
setFrameShadow( QFrame::Plain );

mLayout = new QGridLayout( this );
mLayout->setContentsMargins( 9, 1, 9, 1 );
setLayout( mLayout );

mLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 0, 1, 1, 1 );

mCloseBtn = new QToolButton( this );
mCloseBtn->setText( tr( "Close" ) );
mCloseBtn->setIcon( QgsApplication::getThemeIcon( "mIconClose.png" ) );
connect( mCloseBtn, SIGNAL( clicked() ), this, SLOT( popWidget() ) );
mLayout->addWidget( mCloseBtn, 0, 2, 1, 1 );

// start hidden
setVisible( false );
}

QgsMessageBar::~QgsMessageBar()
{
}

void QgsMessageBar::popItem( QgsMessageBarItem *item )
{
Q_ASSERT( item );

if ( item != mCurrentItem && !mList.contains( item ) )
return;

if ( item == mCurrentItem )
{
if ( mCurrentItem )
{
mLayout->removeWidget( mCurrentItem->widget() );
mCurrentItem->widget()->hide();
mCurrentItem = 0;
}

if ( !mList.isEmpty() )
{
pushItem( mList.first() );
}
else
{
hide();
}
}
else
{
mList.removeOne( item );
}

emit widgetRemoved( item->widget() );
}

bool QgsMessageBar::popWidget( QWidget *widget )
{
if ( !widget || !mCurrentItem )
return false;

if ( widget == mCurrentItem->widget() )
{
popItem( mCurrentItem );
return true;
}

foreach( QgsMessageBarItem *item, mList )
{
if ( item->widget() == widget )
{
mList.removeOne( item );
delete item;
return true;
}
}

return false;
}

bool QgsMessageBar::popWidget()
{
if ( !mCurrentItem )
return false;

QgsMessageBarItem *item = mCurrentItem;
popItem( item );
delete item;

return true;
}

void QgsMessageBar::pushItem( QgsMessageBarItem *item )
{
Q_ASSERT( item );

if ( item == mCurrentItem )
return;

if ( mList.contains( item ) )
mList.removeOne( item );

if ( mCurrentItem )
{
mList.prepend( mCurrentItem );
mLayout->removeWidget( mCurrentItem->widget() );
mCurrentItem->widget()->hide();
}

mCurrentItem = item;
mLayout->addWidget( item->widget(), 0, 0, 1, 1 );
mCurrentItem->widget()->show();

setStyleSheet( item->styleSheet() );
show();
}

void QgsMessageBar::pushWidget( QWidget *widget, int level )
{
QString stylesheet;
if ( level >= 2 )
{
stylesheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } QLabel { color: white; }";
}
else if ( level == 1 )
{
stylesheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } QLabel { color: black; }";
}
else if ( level == 0 )
{
stylesheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } QLabel { color: #2554a1; }";
}
pushWidget( widget, stylesheet );
}

void QgsMessageBar::pushWidget( QWidget *widget, const QString &styleSheet )
{
if ( !widget )
return;

// avoid duplicated widget
popWidget( widget );

pushItem( new QgsMessageBarItem( widget, styleSheet ) );
}

QWidget* QgsMessageBar::createMessage( const QString &title, const QString &text, const QIcon &icon, QWidget *parent )
{
QWidget *widget = new QWidget( parent );

QHBoxLayout *layout = new QHBoxLayout( widget );
layout->setContentsMargins( 0, 0, 0, 0 );

if ( !icon.isNull() )
{
QLabel *lblIcon = new QLabel( widget );
lblIcon->setPixmap( icon.pixmap( 24 ) );
layout->addWidget( lblIcon );
}

if ( !title.isEmpty() )
{
QLabel *lblTitle = new QLabel( title, widget );
QFont font = lblTitle->font();
font.setBold( true );
lblTitle->setFont( font );
layout->addWidget( lblTitle );
}

QLabel *lblText = new QLabel( text, widget );
layout->addWidget( lblText );

return widget;
}
84 changes: 84 additions & 0 deletions src/app/qgsmessagebar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/***************************************************************************
qgsmessagebar.h - description
-------------------
begin : June 2012
copyright : (C) 2012 by Giuseppe Sucameli
email : sucameli at faunalia dot it
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSMESSAGEBAR_H
#define QGSMESSAGEBAR_H

#include <qgisgui.h>

#include <QString>
#include <QFrame>
#include <QIcon>
#include <QColor>
#include <QList>

class QWidget;
class QGridLayout;
class QToolButton;

/** \ingroup gui
* A bar for displaying non-blocking messages to the user.
* \note added in 1.8
*/
class QgsMessageBar: public QFrame
{
Q_OBJECT
public:
QgsMessageBar( QWidget *parent = 0 );
~QgsMessageBar();

void pushWidget( QWidget *widget, int level = 0 );
void pushWidget( QWidget *widget, const QString &styleSheet );

bool popWidget( QWidget *widget );

static QWidget* createMessage( const QString &text, QWidget *parent = 0 ) { return createMessage( QString::null, text, QIcon(), parent ); }
static QWidget* createMessage( const QString &text, const QIcon &icon, QWidget *parent = 0 ) { return createMessage( QString::null, text, icon, parent ); }
static QWidget* createMessage( const QString &title, const QString &text, QWidget *parent = 0 ) { return createMessage( title, text, QIcon(), parent ); }
static QWidget* createMessage( const QString &title, const QString &text, const QIcon &icon, QWidget *parent = 0 );

signals:
void widgetRemoved( QWidget *widget );

public slots:
bool popWidget();

private:
class QgsMessageBarItem
{
public:
QgsMessageBarItem( QWidget *widget, const QString &styleSheet ):
mWidget( widget ), mStyleSheet( styleSheet ) {}
~QgsMessageBarItem() {}

QWidget* widget() const { return mWidget; }
QString styleSheet() const { return mStyleSheet; }

private:
QWidget *mWidget;
QString mStyleSheet;
};

void popItem( QgsMessageBarItem *item );
void pushItem( QgsMessageBarItem *item );

QgsMessageBarItem *mCurrentItem;
QList<QgsMessageBarItem *> mList;
QToolButton *mCloseBtn;
QGridLayout *mLayout;
};

#endif
33 changes: 32 additions & 1 deletion src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
mStyle = QgsStyleV2::defaultStyle();
populateStyles();

// Project macros
QString pythonMacros = QgsProject::instance()->readEntry( "Macros", "/pythonCode", QString::null );
grpPythonMacros->setChecked( !pythonMacros.isEmpty() );
if ( !pythonMacros.isEmpty() )
{
ptePythonMacros->setPlainText( pythonMacros );
}
else
{
resetPythonMacros();
}

restoreState();
}

Expand Down Expand Up @@ -565,6 +577,19 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( "DefaultStyles", "/AlphaInt", 255 - mTransparencySlider->value() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );

// store project macros
QString pythonMacros;
if ( grpPythonMacros->isChecked() )
{
pythonMacros = ptePythonMacros->toPlainText();
}
else
{
pythonMacros = QString::null;
resetPythonMacros();
}
QgsProject::instance()->writeEntry( "Macros", "/pythonCode", pythonMacros );

//todo XXX set canvas color
emit refresh();
}
Expand Down Expand Up @@ -947,4 +972,10 @@ void QgsProjectProperties::editSymbol( QComboBox* cbo )
cbo->setItemIcon( cbo->currentIndex(), icon );
}


void QgsProjectProperties::resetPythonMacros()
{
grpPythonMacros->setChecked( false );
ptePythonMacros->setPlainText( "def openProject():\n pass\n\n" \
"def saveProject():\n pass\n\n" \
"def closeProject():\n pass\n" );
}
5 changes: 5 additions & 0 deletions src/app/qgsprojectproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
*/
void restoreState();

/*!
* Reset the python macros
*/
void resetPythonMacros();

long mProjectSrsId;
long mLayerSrsId;
};
36 changes: 34 additions & 2 deletions src/ui/qgsprojectpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>660</width>
<height>792</height>
<width>670</width>
<height>812</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down Expand Up @@ -1106,6 +1106,38 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Macros</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0">
<widget class="QGroupBox" name="grpPythonMacros">
<property name="title">
<string>Python macros</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPlainTextEdit" name="ptePythonMacros">
<property name="documentTitle">
<string notr="true"/>
</property>
<property name="plainText">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down