UltraScan III
us_long_messagebox.cpp
Go to the documentation of this file.
1 
3 #include "us_long_messagebox.h"
4 #include "us_gui_settings.h"
5 #include "us_settings.h"
6 
7 // Main constructor with title and message
9  const QString& message,
10  QWidget* parent,
11  Qt::WindowFlags f )
12  :US_WidgetsDialog( parent, f )
13 {
14  setAttribute ( Qt::WA_DeleteOnClose );
15  setWindowTitle( title );
16  setPalette ( US_GuiSettings::frameColor() );
17 
18  // Main layout
19  QVBoxLayout* main = new QVBoxLayout( this );
20  main->setContentsMargins( 2, 2, 2, 2 );
21  main->setSpacing ( 2 );
22 
23  // Create the text box
24  textbox = us_textedit();
25  textbox->setFrameStyle( QFrame::NoFrame || QFrame::Plain );
26  textbox->setFont( QFont( US_GuiSettings::fontFamily(),
27  US_GuiSettings::fontSize() - 2 ) );
28  textbox->setPlainText( message );
29  adjustSize();
30  textbox->show();
31 
32  // Create a button box with single OK button
33  QDialogButtonBox* btnbox = new QDialogButtonBox( QDialogButtonBox::Ok );
34  btnbox->setCenterButtons( true );
35  connect( btnbox, SIGNAL( accepted() ),
36  this, SLOT( close_diag() ) );
37 
38  main->addWidget( textbox );
39  main->addWidget( btnbox );
40 }
41 
42 // Set the title string
43 void US_LongMessageBox::setTitle( const QString& title )
44 {
45  setWindowTitle( title );
46 }
47 
48 // Set new plain text
49 void US_LongMessageBox::setText( const QString& message )
50 {
51  textbox->setPlainText( message );
52  adjustSize();
53 }
54 
55 // Set new rich text
56 void US_LongMessageBox::setHtml( const QString& message )
57 {
58  textbox->setAcceptRichText( true );
59  textbox->setHtml( message );
60  adjustSize();
61 }
62 
63 // Close
65 {
66  accept();
67 }
68 
69 // Overloaded method to adjust size of message box
71 {
72  textbox->adjustSize();
73  QWidget::adjustSize();
74  QSize wsize = frameSize(); // message box size, currently
75  QSize tsize = textbox->frameSize(); // text box size
76  QSize bsize = wsize - tsize; // border dimensions
77 
78  if ( tsize.width() > wsize.width() || tsize.height() > wsize.height() )
79  bsize = QSize( 4, 32 ); // minimum border dimensions
80 
81  QStringList alltext = textbox->toPlainText().split( "\n" ); // text lines
82  int nline = alltext.size();
83  int longx = -1;
84  int maxc = -1;
85 
86  for ( int ii = 0; ii < nline; ii++ )
87  { // find the longest line and save its index
88  int llen = alltext[ ii ].length();
89 
90  if ( llen > maxc )
91  {
92  maxc = llen;
93  longx = ii;
94  }
95  }
96 
97  // Determine the size needed for current text in current font
98  QFontMetrics fm( textbox->font() );
99  int fontw = fm.width( alltext[ longx ] ) + fm.width( "WW" );
100  int fonth = fm.lineSpacing() * ( nline + 3 );
101  fontw = ( ( fontw / 20 + 1 ) * 20 );
102  fonth = ( ( fonth / 20 + 1 ) * 20 );
103 
104  tsize = QSize( fontw, fonth );
105  wsize = tsize + bsize;
106 
107  textbox->resize( tsize ); // re-size text box
108  resize( wsize ); // re-size dialog
109 }
110