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