Logo Search packages:      
Sourcecode: qasmixer version File versions  Download package

ds_check_button_painter_svg.cpp

//
// C++ Implementation:
//
// Description:
//
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2010-2011
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "ds_check_button_painter_svg.hpp"

#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QSvgRenderer>

#include <iostream>


namespace Wdg
{
namespace Painter
{


DS_Check_Button_Painter_SVG::DS_Check_Button_Painter_SVG ( )
{
      _suffix_bg[0] = "idle";
      _suffix_bg[1] = "focus";
      _suffix_bg[2] = "hover_idle";
      _suffix_bg[3] = "hover_focus";

      _suffix_handle[0] = "unchecked_idle";
      _suffix_handle[1] = "unchecked_focus";
      _suffix_handle[2] = "unchecked_hover_idle";
      _suffix_handle[3] = "unchecked_hover_focus";

      _suffix_handle[4] = "checked_idle";
      _suffix_handle[5] = "checked_focus";
      _suffix_handle[6] = "checked_hover_idle";
      _suffix_handle[7] = "checked_hover_focus";

      _suffix_handle[8] = "half_checked_focus";
      _suffix_handle[9] = "half_checked_hover_focus";
}


void
DS_Check_Button_Painter_SVG::clear_pics_bg ( )
{
      for ( unsigned int sii=0; sii < num_bg_images; ++sii ) {
            _pics_bg[sii] = QPicture();
      }
}
00058 

void
DS_Check_Button_Painter_SVG::clear_pics_handle ( )
{
      for ( unsigned int sii=0; sii < num_handle_images; ++sii ) {
            _pics_handle[sii] = QPicture();
      }
}


bool
DS_Check_Button_Painter_SVG::load_bg (
      const QString & base_dir_n,
      const QString & file_prefix_n )
{
      bool fail ( false );

      clear_pics_bg();

      QStringList svg_lst;
      {
            const QStringList all_lst (
                  file_list ( base_dir_n, file_prefix_n + "*.svg" ) );
            for ( unsigned int iidx=0; iidx < num_bg_images; ++iidx ) {
                  unsigned int sidx ( num_bg_images );
                  {
                        const QString sstr ( file_prefix_n + _suffix_bg[iidx] + ".svg" );
                        for ( int lii=0; lii < all_lst.size(); ++lii ) {
                              if ( all_lst[lii].contains ( sstr, Qt::CaseInsensitive ) ) {
                                    sidx = lii;
                                    break;
                              }
                        }
                  }
                  if ( sidx < num_bg_images ) {
                        svg_lst.append ( all_lst[sidx] );
                  } else {
                        fail = true;
                        break;
                  }
            }
      }

      // Render
      if ( svg_lst.size() == (int)num_bg_images ) {
            for ( unsigned int iidx=0; iidx < num_bg_images; ++iidx ) {
                  if ( render_svg ( svg_lst[iidx], _pics_bg[iidx] ) ) {
                        fail = true;
                        break;
                  }
00109             }
      }

      if ( fail ) {
            clear_pics_bg();
      }

      return fail;
}


bool
DS_Check_Button_Painter_SVG::load_handle (
      const QString & base_dir_n,
      const QString & file_prefix_n )
{
      bool fail ( false );

      clear_pics_handle();

      QStringList svg_lst;
      {
            const QStringList all_lst (
                  file_list ( base_dir_n, file_prefix_n + "*.svg" ) );
            for ( unsigned int iidx=0; iidx < num_handle_images; ++iidx ) {
                  unsigned int sidx ( num_handle_images );
                  {
                        const QString sstr ( file_prefix_n + _suffix_handle[iidx] + ".svg" );
                        for ( int lii=0; lii < all_lst.size(); ++lii ) {
                              if ( all_lst[lii].contains ( sstr, Qt::CaseInsensitive ) ) {
                                    sidx = lii;
                                    break;
                              }
                        }
                  }
                  if ( sidx < num_handle_images ) {
                        svg_lst.append ( all_lst[sidx] );
                  } else {
                        fail = true;
                        break;
                  }
            }
      }

      // Render
      if ( svg_lst.size() == (int)num_handle_images ) {
            for ( unsigned int iidx=0; iidx < num_handle_images; ++iidx ) {
                  if ( render_svg ( svg_lst[iidx], _pics_handle[iidx] ) ) {
                        fail = true;
                        break;
                  }
00160             }
      }

      if ( fail ) {
            clear_pics_handle();
      }

      return fail;
}


bool
DS_Check_Button_Painter_SVG::load_bg_and_handle (
      const QString & base_dir_n,
      const QString & bg_prefix_n,
00175       const QString & handle_prefix_n )
{
      bool fail;
      fail = load_bg ( base_dir_n, bg_prefix_n );
      if ( !fail ) {
            fail = load_handle ( base_dir_n, handle_prefix_n );
      }
      return fail;
}


bool
DS_Check_Button_Painter_SVG::render_svg (
      const QString & svg_file_n,
      QPicture & pict_n )
{
      bool fail ( false );
      QSvgRenderer svg_render;
      if ( svg_render.load ( svg_file_n ) ) {
            pict_n.setBoundingRect (
                  QRect ( QPoint ( 0, 0 ), svg_render.defaultSize() ) );
            QPainter pnt ( &pict_n );
            svg_render.render ( &pnt );
      } else {
            std::cerr << "SVG file loading failed on " << "\n";
            std::cerr << svg_file_n.toLocal8Bit().data() << "\n";
            fail = true;
      }

      return fail;
}


QStringList
DS_Check_Button_Painter_SVG::file_list (
      const QString & base_dir_n,
      const QString & file_filter_n )
{
      QStringList res;

      QString base_dir ( base_dir_n );
      if ( base_dir[base_dir.size() - 1] != '/' ) {
            base_dir.append ( '/' );
      }

      QDir dir ( base_dir );
      if ( dir.exists() ) {
            dir.setSorting ( QDir::Name | QDir::IgnoreCase );
            dir.setFilter ( QDir::Files | QDir::Readable );
            {
                  QStringList lst;
                  lst.append ( file_filter_n );
                  dir.setNameFilters ( lst );
            }
00229             res = dir.entryList();
      }

      for ( int ii=0; ii < res.size(); ++ii ) {
            res[ii] = res[ii].prepend ( base_dir );
      }

      return res;
}


int
DS_Check_Button_Painter_SVG::paint_bg (
      Shared_Pixmaps_Set * spms_n )
{
      if ( spms_n->pixmap_size().isValid() ) {
            set_is ( spms_n->pixmap_size() );
00246             for ( unsigned int sii=0; sii < num_bg_images; ++sii ) {
                  paint_picture (
                        spms_n->pixmap_ref ( sii ),
                        _pics_bg[sii] );
            }
            return 0;
      }
      return -1;
}


int
DS_Check_Button_Painter_SVG::paint_handle (
      Shared_Pixmaps_Set * spms_n )
{
      if ( spms_n->pixmap_size().isValid() ) {
            set_is ( spms_n->pixmap_size() );
            for ( unsigned int sii=0; sii < num_handle_images; ++sii ) {
                  paint_picture (
                        spms_n->pixmap_ref ( sii ),
                        _pics_handle[sii] );
            }
            return 0;
      }
      return -1;
}


void
DS_Check_Button_Painter_SVG::paint_picture (
      QPixmap & pxmap_n,
      const QPicture & pict_n )
{
      QImage img ( is(), QImage::Format_ARGB32 );
      img.fill ( 0 );

      {
            QPainter pnt ( &img );
            pnt.setRenderHints (
                  QPainter::Antialiasing |
                  QPainter::TextAntialiasing |
                  QPainter::SmoothPixmapTransform );
            pnt.setBrush ( Qt::NoBrush );
            pnt.setPen ( Qt::NoPen );

            {
                  const QSize bsize ( pict_n.boundingRect().size() );
                  if ( is() != bsize ) {
                        pnt.scale (
                              double ( is().width() / double ( bsize.width() ) ),
                              double ( is().height() / double ( bsize.height() ) ) );
                  }
            }
            pnt.drawPicture ( QPoint ( 0, 0 ), pict_n );
      }

      pxmap_n = QPixmap::fromImage ( img );
}


} // End of namespace
} // End of namespace

Generated by  Doxygen 1.6.0   Back to index