]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormUrl.C
Fixed connections. There is still a bug somewhere.
[lyx.git] / src / frontends / qt2 / FormUrl.C
1 /*
2  * FormUrl.C
3  * (C) 2000 LyX Team
4  * John Levon, moz@compsoc.man.ac.uk
5  */
6  
7 /***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15
16 #include <config.h>
17
18 #include "FormUrlDialog.h"
19 #undef emit
20
21 #include "Dialogs.h"
22 #include "FormUrl.h"
23 #include "gettext.h"
24 #include "buffer.h"
25 #include "LyXView.h"
26 #include "lyxfunc.h" 
27
28 #include <qlineedit.h>
29 #include <qcheckbox.h>
30 #include <qpushbutton.h>
31
32 FormUrl::FormUrl(LyXView *v, Dialogs *d)
33         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0)
34 {
35         // let the dialog be shown
36         // This is a permanent connection so we won't bother
37         // storing a copy because we won't be disconnecting.
38         d->showUrl.connect(slot(this, &FormUrl::showUrl));
39         d->createUrl.connect(slot(this, &FormUrl::createUrl));
40 }
41
42 FormUrl::~FormUrl()
43 {
44         delete dialog_;
45 }
46
47 void FormUrl::showUrl(InsetCommand * const inset)
48 {
49         // FIXME: when could inset be 0 here ?
50         if (inset==0)
51                 return;
52
53         inset_ = inset;
54         readonly = lv_->buffer()->isReadonly();
55         ih_ = inset_->hide.connect(slot(this,&FormUrl::hide));
56         params = inset->params();
57         
58         show();
59 }
60  
61 void FormUrl::createUrl(string const & arg)
62 {
63         // we could already be showing a URL, clear it out
64         if (inset_)
65                 close();
66  
67         readonly = lv_->buffer()->isReadonly();
68         params.setFromString(arg);
69         show();
70 }
71  
72 void FormUrl::update()
73 {
74         dialog_->urlED->setText(params.getContents().c_str());
75         dialog_->nameED->setText(params.getOptions().c_str());
76
77         if (params.getCmdName()=="url") 
78                 dialog_->hyperlinkCB->setChecked(false);
79         else
80                 dialog_->hyperlinkCB->setChecked(true);
81
82         if (readonly) {
83                 dialog_->nameED->setFocusPolicy(QWidget::NoFocus);
84                 dialog_->urlED->setFocusPolicy(QWidget::NoFocus);
85                 dialog_->okPB->setEnabled(false);
86                 dialog_->cancelPB->setText(_("Close"));
87                 dialog_->hyperlinkCB->setEnabled(false);
88         } else {
89                 dialog_->nameED->setFocusPolicy(QWidget::StrongFocus);
90                 dialog_->urlED->setFocusPolicy(QWidget::StrongFocus);
91                 dialog_->urlED->setFocus();
92                 dialog_->okPB->setEnabled(true);
93                 dialog_->cancelPB->setText(_("Cancel"));
94                 dialog_->hyperlinkCB->setEnabled(true);
95         }
96 }
97  
98 void FormUrl::apply()
99 {
100         if (readonly)
101                 return;
102
103         params.setContents(dialog_->urlED->text().latin1());
104         params.setOptions(dialog_->nameED->text().latin1());
105
106         if (dialog_->hyperlinkCB->isChecked())
107                 params.setCmdName("htmlurl");
108         else
109                 params.setCmdName("url");
110
111         if (inset_ != 0) {
112                 if (params != inset_->params()) {
113                         inset_->setParams(params);
114                         lv_->view()->updateInset(inset_, true);
115                 }
116         } else
117                 lv_->getLyXFunc()->Dispatch(LFUN_INSERT_URL, params.getAsString().c_str());
118 }
119  
120 void FormUrl::show()
121 {
122         if (!dialog_)
123                 dialog_ = new FormUrlDialog(this, 0, _("LyX: Url"), false);
124  
125         if (!dialog_->isVisible()) {
126                 h_ = d_->hideBufferDependent.connect(slot(this, &FormUrl::hide));
127                 u_ = d_->updateBufferDependent.connect(slot(this, &FormUrl::update));
128         }
129
130         dialog_->raise();
131         dialog_->setActiveWindow();
132  
133         update();
134         dialog_->show();
135 }
136
137 void FormUrl::close()
138 {
139         h_.disconnect();
140         u_.disconnect();
141         ih_.disconnect();
142         inset_ = 0;
143 }
144  
145 void FormUrl::hide()
146 {
147         dialog_->hide();
148         close();
149 }