picking up branches/tmp/sh/qt4-port/, merging it with trunk
[duncan/yast2-qt4.git] / src / pkg / YQPkgSelDescriptionView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgSelDescriptionView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-pkg"
21 #include <ycp/y2log.h>
22
23 #include <unistd.h>
24 #include <qregexp.h>
25
26 #include "YQPkgSelDescriptionView.h"
27 #include "YQUI.h"
28 #include "YQi18n.h"
29 #include "utf8.h"
30
31 using std::list;
32 using std::string;
33
34
35 YQPkgSelDescriptionView::YQPkgSelDescriptionView( QWidget * parent )
36     : YQPkgDescriptionView( parent )
37 {
38 }
39
40
41 YQPkgSelDescriptionView::~YQPkgSelDescriptionView()
42 {
43     // NOP
44 }
45
46
47 void
48 YQPkgSelDescriptionView::showDetails( ZyppSel selectable )
49 {
50     _selectable = selectable;
51
52     if ( ! selectable )
53     {
54         clear();
55         return;
56     }
57
58     QString html_text = htmlHeading( selectable );
59
60     QString description = fromUTF8( selectable->theObj()->description() );
61
62     if ( ! description.contains( "<!-- DT:Rich -->" ) )
63         description = simpleHtmlParagraphs( description );
64
65     html_text += description;
66
67     setTextFormat( Qt::RichText );
68     setText( html_text );
69     ensureVisible( 0, 0 );      // Otherwise hyperlinks will be centered
70 }
71
72
73
74 QString
75 YQPkgSelDescriptionView::htmlHeading( ZyppSel selectable )
76 {
77     ZyppObj             zyppObj         = selectable->theObj();
78     ZyppSelection       selection       = tryCastToZyppSelection( zyppObj );
79     ZyppPattern         pattern         = tryCastToZyppPattern  ( zyppObj );
80
81     if ( ! selection && ! pattern )
82         return YQPkgGenericDetailsView::htmlHeading( selectable );
83
84     QString summary = fromUTF8( zyppObj->summary() );
85     bool useBigFont = ( summary.length() <= 40 );
86
87     if ( summary.isEmpty() )                    // No summary?
88         summary = fromUTF8( zyppObj->name() );  // Use name instead (internal only normally)
89
90     QString icon = pattern ? pattern->icon().asString().c_str() : "";
91
92     if ( icon.isEmpty() )
93     {
94         icon = zyppObj->name().c_str();
95         icon.replace( ' ', '_' );
96     }
97
98     if ( ! icon.isEmpty() )
99     {
100         if ( icon.startsWith( "./" ) )
101             icon.replace( QRegExp( "^\\./" ), "" );
102             
103         if ( ! icon.endsWith( ".png", false ) &&
104              ! icon.endsWith( ".jpg", false )   )
105             icon += ".png";
106
107         QString origIconName = icon;
108
109         if ( ! icon.contains( "/" ) )           // no path at all
110         {
111             // Look in icon directories:
112             //
113             //   /usr/share/YaST2/theme/current/icons/32x32/apps/
114             //   /usr/share/YaST2/theme/current/icons/48x48/apps/
115
116             QString iconBaseName = icon;
117             icon = findIcon( QString( THEMEDIR ) + "/icons/32x32/apps/" + iconBaseName );
118
119             if ( icon.isEmpty() )
120                 icon = findIcon( QString( THEMEDIR ) + "/icons/48x48/apps/" + iconBaseName );
121         }
122         else if ( ! icon.startsWith( "/" ) )    // relative path
123         {
124             // Use path relative to theme directory:
125             //
126             //   /usr/share/YaST2/theme/current/ + icon
127
128             icon = findIcon( QString( THEMEDIR ) + "/" + icon );
129         }
130
131         if ( pattern && icon.isEmpty() )
132             y2warning( "No icon for pattern %s - icon name: %s",
133                        zyppObj->name().c_str(), (const char *) origIconName );
134     }
135
136
137     QString html = "<table width=100%";
138
139     if ( ! YQUI::ui()->usingVisionImpairedPalette() )
140         html += " bgcolor=#C8C8F8";     // or #E0E0F8 (very light blueish grey)
141
142     html += "><tr><td>"
143         + ( useBigFont ? QString( "<h2>" ) : QString( "<b>" ) )
144         + summary
145         + ( useBigFont ? QString( "</h2>" ) : QString( "</b>" ) )
146         + "</td></tr>"
147         + "</table>";
148
149     if ( ! icon.isEmpty() )
150     {
151         html = QString( "<table width=100%><tr>" )
152             + "<td><img src=\"" + icon + "\"></td>"
153             + "<td width=100%>" + html + "</td>"
154             + "</tr></table>";
155     }
156
157     return html;
158 }
159
160
161
162 QString
163 YQPkgSelDescriptionView::findIcon( const QString & icon ) const
164 {
165     if ( access( icon, R_OK ) == 0 )
166     {
167         y2debug( "Found icon %s", (const char *) icon );
168         return icon;
169     }
170     else
171     {
172         y2debug( "No icon %s", (const char *) icon );
173         return "";
174     }
175 }
176
177
178 #include "YQPkgSelDescriptionView.moc"