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