GUISettings.cpp

IdĽ do dokumentacji tego pliku.
00001 #include "GUISettings.h"
00002 
00003 #include <QTextCodec>
00004 #include <QHostInfo>
00005 #include <QPushButton>
00006 #include <QPalette>
00007 #include <QIcon>
00008 #include <QPixmap>
00009 #include <QStringList>
00010 #include <QMessageBox>
00011 
00012 
00013 GUISettings::GUISettings(QWidget *parent):QDialog(parent)
00014 {
00015         
00016         QTextCodec::setCodecForCStrings( QTextCodec::codecForName("Windows-1250") );
00017         QTextCodec::setCodecForTr( QTextCodec::codecForName("Windows-1250") );
00018         settings = new QSettings("settings.ini",QSettings::IniFormat,this);
00019         settings->setFallbacksEnabled(false);
00020         if(settings->status() == QSettings::NoError)
00021         {
00022                 try
00023                 {
00024                         this->readSettings();
00025                 }
00026                 catch(QString errorMsg)
00027                 {
00028                         printErrorMsg(tr("Brak pliku konfiguracyjnego lub plik uszkodzony"
00029                                                         "\n"
00030                                                         "Ładuje ustawienia domyślne" ));
00031                         settings->endGroup();
00032                         defaultSettings();
00033                         saveToFile();
00034                 }
00035         }
00036         else printErrorMsg(tr("Błąd związany z plikiem konfiguracyjnym"));
00037         
00038         if(opacity_enabled)
00039         {
00040                 this->setAutoFillBackground(false);
00041         this->setAttribute(Qt::WA_NoSystemBackground, false);
00042                 this->setWindowOpacity(opacity);
00043         }
00044         
00045         
00046         QPushButton *okButton = new QPushButton(tr("Zatwierdz"));
00047     QPushButton *defaultButton = new QPushButton(tr("Domyślne"));
00048     QPushButton *cancelButton = new QPushButton(tr("Anuluj"));
00049 
00050     connect(okButton, SIGNAL(clicked()), this, SLOT(saveSettings()));
00051     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
00052     connect(defaultButton, SIGNAL(clicked()), this, SLOT(setDefaultSettings()));
00053 
00054     QHBoxLayout *buttonLayout = new QHBoxLayout;
00055     buttonLayout->addStretch(1);
00056     buttonLayout->addWidget(okButton);
00057     buttonLayout->addWidget(defaultButton);
00058         buttonLayout->addWidget(cancelButton);
00059         
00060         
00061         
00062         
00063         QVBoxLayout *mainLayout = new QVBoxLayout;
00064         
00065         QGroupBox *network= new QGroupBox(tr("Sieć"));
00066         QVBoxLayout *networkGroupLayout = new QVBoxLayout(network);
00067     
00068     QHBoxLayout *nickLayout = new QHBoxLayout;
00069         nick_label = new QLabel(tr("Nick"));
00070         nick_line_edit = new QLineEdit(user_nick);
00071     nick_line_edit->setMaxLength(15);
00072         nick_line_edit->setWhatsThis(tr("Nazwa użytkownika, pod nią bedziesz widziany przez innych użytkowników programu."
00073                                                                         "Nie dłuższa niż 15 znaków."));
00074         nickLayout->addWidget(nick_label);
00075         nickLayout->addSpacing(65);
00076     nickLayout->addWidget(nick_line_edit);
00077         
00078         QHBoxLayout *broadcastLayout = new QHBoxLayout;
00079         broadcast_label = new QLabel(tr("Broadcast"));
00080     broadcast_line_edit = new QLineEdit(broadcast.toString());
00081     broadcast_line_edit->setInputMask ( "000.000.000.000; ");
00082     broadcast_line_edit->setWhatsThis(tr("Adres rozgłoszeniowy, niezbędny do połączenia programu i rozmów publicznych."));
00083         broadcastLayout->addWidget(broadcast_label);
00084         broadcastLayout->addSpacing(39);
00085         broadcastLayout->addWidget(broadcast_line_edit);
00086         
00087         QHBoxLayout *directoryLayout = new QHBoxLayout;
00088     directory_label = new QLabel(tr("Zapisz plik w.."));
00089         directory_line_edit = new QLineEdit(file_storage_path);
00090         directory_line_edit->setReadOnly(true);
00091         directory_line_edit->setWhatsThis(tr("Domyślnie tu zapisywane bedą nadchodzące pliki"));
00092         directory_button = new QPushButton(tr("Ścieżka.."));
00093         connect(directory_button, SIGNAL(clicked()), this, SLOT(setExistingDirectory()));
00094         directoryLayout->addWidget(directory_label);
00095         directoryLayout->addSpacing(20);
00096         directoryLayout->addWidget(directory_line_edit);
00097         directoryLayout->addWidget(directory_button);
00098         
00099         networkGroupLayout->addLayout(nickLayout);
00100         networkGroupLayout->addLayout(broadcastLayout);
00101         networkGroupLayout->addLayout(directoryLayout);
00102 
00103         QGroupBox *appearance= new QGroupBox(tr("Wygląd"));
00104         QVBoxLayout *appearanceGroupLayout = new QVBoxLayout(appearance);
00105         
00106         QHBoxLayout *backgroundColorLayout = new QHBoxLayout;
00107         background_label = new QLabel(tr("Kolor tła"));
00108         background_button = new QPushButton("");
00109         background_button->setWhatsThis(tr("Ustaw kolor tła dla listy użytkowników i okienek rozmowy"));
00110         this->setButtonBackground(background_button, background_color); 
00111         connect(background_button, SIGNAL(clicked()), this, SLOT(setBackgroundColor()));
00112         backgroundColorLayout->addWidget(background_label);
00113         backgroundColorLayout->addWidget(background_button);
00114         
00115         QHBoxLayout *systemColorMsgLayout = new QHBoxLayout;
00116         font_system_color_msg_label = new QLabel(tr("Kolor komunikatów systemowych"));
00117         font_system_color_msg_button = new QPushButton("");
00118         font_system_color_msg_button->setWhatsThis(tr("Ustaw kolor komunikatów systemowych"));
00119         this->setButtonBackground(font_system_color_msg_button, font_color_system_msg);
00120         connect(font_system_color_msg_button, SIGNAL(clicked()), this, SLOT(setFontColorSystemMsg()));
00121         systemColorMsgLayout->addWidget(font_system_color_msg_label);
00122         systemColorMsgLayout->addWidget(font_system_color_msg_button);
00123         
00124         QHBoxLayout *systemUserColorMsgLayout = new QHBoxLayout;
00125         font_system_color_user_msg_label = new QLabel(tr("Kolor komunikatów użytkownika"));
00126         font_system_color_user_msg_button = new QPushButton("");
00127         font_system_color_user_msg_button->setWhatsThis(tr("Ustaw kolor komunikatów wyświetlanych w odpowiedzi "
00128                                                                                                                 "na działanie użytkownika (np. ping, sprawdź Ip)"));
00129         this->setButtonBackground(font_system_color_user_msg_button, font_color_system_user_msg);
00130         connect(font_system_color_user_msg_button, SIGNAL(clicked()), this, SLOT(setFontColorSystemUserMsg()));
00131         systemUserColorMsgLayout->addWidget(font_system_color_user_msg_label);
00132         systemUserColorMsgLayout->addWidget(font_system_color_user_msg_button);
00133         
00134         QHBoxLayout *fontColorLayout = new QHBoxLayout;
00135         font_color_label = new QLabel(tr("Kolor wiadomości"));
00136         font_color_button = new QPushButton("");
00137         font_color_button->setWhatsThis(tr("Ustaw domyślny kolor dla wysyłanych wiadomości w trakcie rozmowy "));
00138         this->setButtonBackground(font_color_button, font_color);
00139         connect(font_color_button, SIGNAL(clicked()), this, SLOT(setFontColor()));
00140         fontColorLayout->addWidget(font_color_label);
00141         fontColorLayout->addWidget(font_color_button);
00142         
00143         opticGroup = new QGroupBox(tr("Przezroczystość [Beta]"));
00144         opticBoxLayout = new QHBoxLayout(opticGroup);
00145         opticGroup->setCheckable(true);
00146         opticGroup->setChecked(opacity_enabled);
00147         connect(opticGroup,SIGNAL(toggled(bool)),this,SLOT(setOpacityEnabled(bool)));
00148         opacity_label = new QLabel(tr("Przezroczystość (musi obsługiwać system)"));
00149         opacity_box = new QDoubleSpinBox;
00150         connect(opacity_box,SIGNAL(valueChanged(double)),this,SLOT(opacityChanged(double)));
00151         opacity_box->setSingleStep(0.01);
00152         opacity_box->setRange(0.4,1);
00153         opacity_box->setValue(opacity);
00154         
00155         opticBoxLayout->addWidget(opacity_label);
00156         opticBoxLayout->addWidget(opacity_box);
00157         
00158         appearanceGroupLayout->addLayout(backgroundColorLayout);
00159         appearanceGroupLayout->addLayout(systemColorMsgLayout);
00160         appearanceGroupLayout->addLayout(systemUserColorMsgLayout);
00161         appearanceGroupLayout->addLayout(fontColorLayout);
00162         appearanceGroupLayout->addWidget(opticGroup);
00163         
00164     mainLayout->addWidget(network);
00165     mainLayout->addWidget(appearance);
00166     mainLayout->addStretch(1);
00167         mainLayout->addLayout(buttonLayout);
00168     setLayout(mainLayout);
00169     
00170    
00171         #ifdef Q_WS_X11
00172                 setFixedSize(400, 490);
00173         #endif
00174 
00175         #ifdef Q_WS_WIN
00176                 setFixedSize(400, 400);
00177         #endif
00178  
00179         
00180     setWindowTitle(tr("Ustawienia"));
00181         setWindowIcon(QIcon("icon/MainToolBar/configure.png"));
00182 }
00183 
00184 GUISettings::~GUISettings()
00185 {
00186         delete nick_label;
00187     delete nick_line_edit;
00188     
00189         delete broadcast_label;
00190     delete broadcast_line_edit;
00191     
00192     delete directory_line_edit;      
00193     delete directory_label;
00194     delete directory_button;
00195     
00196     delete font_color_label;
00197     delete font_system_color_msg_label;
00198     delete font_system_color_user_msg_label;
00199     delete background_label;
00200     delete opacity_label;
00201     delete font_color_button;
00202     delete font_system_color_msg_button;
00203     delete font_system_color_user_msg_button;
00204     delete background_button;
00205     delete opacity_box;
00206     
00207     delete okButton;
00208     delete defaultButton;
00209     delete cancelButton;
00210     
00211         delete opticBoxLayout;
00212         delete opticGroup;
00213         delete settings;
00214 }
00215 
00216 void GUISettings::defaultSettings()
00217 {
00218         user_nick = QHostInfo::localHostName();
00219         broadcast = QHostAddress("255.255.255.255");
00220         port = 2310;
00221         file_storage_path = "";
00222         background_color = Qt::white;
00223         font_color = Qt::black;
00224         font_color_system_msg = Qt::red;
00225         font_color_system_user_msg = Qt::green;
00226         opacity_enabled = false;
00227         opacity = 0.9;
00228 }
00229 void GUISettings::setDefaultSettings()
00230 {
00231 //      this->defaultSettings();
00232         nick_line_edit->setText(QHostInfo::localHostName());
00233         broadcast_line_edit->setText("255.255.255.255");
00234         directory_line_edit->setText("");
00235         this->setButtonBackground(background_button, Qt::white);
00236         this->setButtonBackground(font_system_color_msg_button, Qt::red);
00237         this->setButtonBackground(font_system_color_user_msg_button, Qt::green);
00238         this->setButtonBackground(font_color_button, Qt::black);
00239         opticGroup->setChecked(false);
00240         opacity_box->setValue(0.9);
00241         this->repaint();
00242 }
00243 void GUISettings::setExistingDirectory()
00244 {
00245     QString directory = QFileDialog::getExistingDirectory(this,
00246                                 tr("Katalog domyślny | Wybierz Folder"),
00247                                 directory_line_edit->text(),
00248                                 QFileDialog::DontResolveSymlinks
00249                                 | QFileDialog::ShowDirsOnly);
00250     
00251         if (!directory.isEmpty())
00252     {
00253                 directory_line_edit->setText(directory);
00254         }
00255 }
00256 
00257 void GUISettings::setButtonBackground(QPushButton *button, QColor col)
00258 {
00259         QPalette tmp = button->palette();
00260         tmp.setColor(QPalette::Button, col);
00261         QPixmap tmpI(32,10);
00262         tmpI.fill(col); 
00263         button->setPalette(tmp);
00264         button->setIcon(QIcon(tmpI));
00265 }
00266 
00267 
00268 
00269 void GUISettings::setBackgroundColor()
00270 {
00271         QColor color = QColorDialog::getColor(background_color, this);
00272     if (color.isValid()) 
00273         {
00274         this->setButtonBackground(background_button, color);   
00275         }
00276 }
00277 void GUISettings::setFontColor()
00278 {
00279         QColor color = QColorDialog::getColor(font_color, this);
00280     if (color.isValid()) 
00281         {
00282         this->setButtonBackground(font_color_button, color);    
00283         }       
00284 }
00285         
00286 void GUISettings::setFontColorSystemMsg()
00287 {
00288         QColor color = QColorDialog::getColor(font_color_system_msg, this);
00289     if (color.isValid()) 
00290         {
00291         this->setButtonBackground(font_system_color_msg_button, color);        
00292         }
00293 }
00294 void GUISettings::setFontColorSystemUserMsg()
00295 {
00296         QColor color = QColorDialog::getColor(font_color_system_user_msg, this);
00297     if (color.isValid()) 
00298         {
00299         this->setButtonBackground(font_system_color_user_msg_button, color);  
00300         }
00301 }
00302 
00303 QColor GUISettings::getColor(const QPushButton *button)
00304 {
00305         return (button->palette().color(QPalette::Button));
00306 }
00307 
00308 QColor GUISettings::getBackgroundColor()
00309 {
00310         return background_color;
00311 }
00312 
00313 QColor GUISettings::getFontColor()
00314 {
00315         return font_color;
00316 }
00317 QColor GUISettings::getFontColorSystemMsg()
00318 {
00319         return font_color_system_msg;
00320 }
00321 QColor GUISettings::getFontColorSystemUserMsg()
00322 {
00323         return font_color_system_user_msg;
00324 }
00325 
00326 QHostAddress GUISettings::getBroadcast()
00327 {
00328         return broadcast;
00329 }
00330 
00331 
00332 unsigned int GUISettings::getPort()
00333 {
00334         return port;
00335 }
00336 
00337 QString GUISettings::getNick()
00338 {
00339         return user_nick;
00340 }
00341 
00342 QString GUISettings::getFileStoragePath()
00343 {
00344         return file_storage_path;
00345 }
00346 void GUISettings::saveToFile()
00347 {
00348         settings->beginGroup("network");
00349         settings->setValue("user_nick",user_nick);
00350         settings->setValue("broadcast",broadcast.toString());
00351         settings->setValue("port",QString::number(port));
00352         settings->setValue("file_storage_path",file_storage_path);
00353         settings->endGroup();
00354         
00355         settings->beginGroup("apperance");
00356         settings->setValue("background_color",background_color.name()); 
00357         settings->setValue("font_color",font_color.name());     
00358         settings->setValue("font_color_system_msg",font_color_system_msg.name());       
00359         settings->setValue("font_color_system_user_msg",font_color_system_user_msg.name());
00360         settings->setValue("opacity_enabled",opacity_enabled);
00361         settings->setValue("opacity",opacity);
00362         settings->endGroup();
00363 }
00364 
00365 void GUISettings::saveSettings()
00366 {
00367         try
00368         {
00369                 if(!this->isValidNick(nick_line_edit->text()))
00370                 {
00371                         throw QString("Nieprawidłowy nick");    
00372                 }
00373                 user_nick = nick_line_edit->text();
00374                 
00375                 if(!this->isValidIp(broadcast_line_edit->text()))
00376                 {
00377                         throw QString("Podany adres broadcast jest nieprawidłowy");
00378                 }
00379                 broadcast = QHostAddress(broadcast_line_edit->text());
00380                 
00381                 file_storage_path = directory_line_edit->text();
00382         
00383                 background_color = getColor(background_button);
00384                 font_color = getColor(font_color_button);
00385                 font_color_system_msg = getColor(font_system_color_msg_button);
00386                 font_color_system_user_msg = getColor(font_system_color_user_msg_button);
00387                 opacity = opacity_box->value();
00388                 this->saveToFile();
00389                 emit settingsChanged();
00390                 QDialog::accept();
00391         }
00392         catch(QString errorMsg)
00393         {
00394                 this->printErrorMsg(errorMsg);
00395         }
00396 }
00397 
00398 void GUISettings::readSettings()
00399 {
00400         QString tmp;
00401         settings->beginGroup("network");
00402         settings->setFallbacksEnabled(false);
00403         tmp = settings->value("user_nick").toString();
00404         if (!isValidNick(tmp)) 
00405                 throw QString(tr("Niepoprawny nick"));
00406         user_nick = tmp;
00407         
00408         tmp = settings->value("broadcast").toString();
00409         if(!isValidIp(tmp))
00410                 throw QString(tr("Niepoprawny adres broadcast"));
00411         broadcast = QHostAddress(tmp);
00412         
00413         unsigned int tmpInt;
00414         tmpInt = settings->value("port","2310").toUInt();
00415         if((tmpInt <= 0) || (tmpInt > 65535))
00416                 throw QString(tr("Numer portu spoza zakresu"));
00417         port = tmpInt;
00418         
00419         tmp = settings->value("file_storage_path").toString();
00420         file_storage_path = tmp;
00421         settings->endGroup();
00422 
00423         QColor tmpColor;
00424         settings->beginGroup("apperance");
00425         tmpColor = settings->value("background_color").value<QColor>();
00426         if(!tmpColor.isValid())
00427                 throw QString(tr("Niepoprawny kolor tła"));
00428         background_color = tmpColor;
00429 
00430         tmpColor = settings->value("font_color").value<QColor>();
00431         if(!tmpColor.isValid())
00432                 throw QString(tr("Niepoprawny kolor domyślnej czcionki"));
00433         font_color = tmpColor;
00434 
00435         tmpColor = settings->value("font_color_system_msg").value<QColor>();
00436         if(!tmpColor.isValid())
00437                 throw QString(tr("Niepoprawny kolor font_color_system_msg"));
00438         font_color_system_msg = tmpColor;
00439 
00440         tmpColor = settings->value("font_color_system_user_msg").value<QColor>();
00441         if(!tmpColor.isValid())
00442                 throw QString(tr("Niepoprawny kolor font_color_system_user_msg"));
00443         font_color_system_user_msg = tmpColor;
00444 
00445         bool tmpBool;
00446         tmpBool = settings->value("opacity_enabled","false").value<bool>();
00447         opacity_enabled = tmpBool;
00448 
00449         double tmpDouble = settings->value("opacity","0.90").toDouble();
00450         if((tmpDouble < 0.4) || (tmpDouble > 1.0))
00451                 throw QString("Wartość przezroczystości spoza zakresu [0.4,1.0]");
00452         opacity = tmpDouble;
00453         settings->endGroup();
00454 }
00455 
00456 void GUISettings::setOpacityEnabled(bool on)
00457 {
00458         if(on)
00459         {
00460                 opacity_enabled = true;
00461                 this->setWindowOpacity(opacity);
00462         }
00463         else
00464         {
00465                 opacity_enabled = false;
00466                 this->setWindowOpacity(1.0);
00467         }
00468 }
00469 
00470 
00471 bool GUISettings::isValidIp(QString ip)
00472 {
00473         if(QHostAddress(ip).isNull())
00474                 return false;
00475         else 
00476                 return true;
00477 
00478 }
00479 
00480 bool GUISettings::isValidNick(QString nick)
00481 {
00482         nick.replace(" ","");
00483         if(nick.isEmpty() || (nick.size()>15))
00484                 return false;
00485         else 
00486                 return true;
00487 }
00488 
00489 void GUISettings::printErrorMsg(const QString &errorMsg)
00490 {
00491         QMessageBox::warning(this,tr("Błąd: "),errorMsg);
00492 }
00493 
00494 void GUISettings::opacityChanged(double op)
00495 {
00496         if(opacity_enabled)
00497                 this->setWindowOpacity(op);
00498         repaint();
00499 }
00500 
00501 bool GUISettings::getOpacityEnabled()
00502 {
00503         return opacity_enabled;
00504 }
00505 
00506 qreal  GUISettings::getOpacity()
00507 {
00508         return opacity;
00509 }

Wygenerowano Sun Jun 11 12:55:08 2006 dla lanChat programem  doxygen 1.4.6