fb2dlgs.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "fb2dlgs.hpp"
  2. #include "fb2code.hpp"
  3. #include "fb2page.hpp"
  4. #include "fb2text.hpp"
  5. #include "fb2tree.hpp"
  6. #include "fb2utils.h"
  7. #include "ui_fb2find.h"
  8. #include "ui_fb2setup.h"
  9. #include <QComboBox>
  10. #include <QDialogButtonBox>
  11. #include <QFileDialog>
  12. #include <QFrame>
  13. #include <QLabel>
  14. #include <QLineEdit>
  15. #include <QTabWidget>
  16. #include <QToolBar>
  17. #include <QWebFrame>
  18. #include <QWebPage>
  19. //---------------------------------------------------------------------------
  20. // FbCodeFindDlg
  21. //---------------------------------------------------------------------------
  22. FbCodeFindDlg::FbCodeFindDlg(FbCodeEdit &edit)
  23. : QDialog(&edit)
  24. , ui(new Ui::FbFind)
  25. , m_edit(edit)
  26. {
  27. ui->setupUi(this);
  28. ui->checkHigh->setText(tr("Complete words"));
  29. connect(ui->btnFind, SIGNAL(clicked()), this, SLOT(find()));
  30. }
  31. FbCodeFindDlg::~FbCodeFindDlg()
  32. {
  33. delete ui;
  34. }
  35. void FbCodeFindDlg::find()
  36. {
  37. QString text = ui->editText->text();
  38. if (text.isEmpty()) return;
  39. QTextDocument::FindFlags options = 0;
  40. if (ui->radioUp->isChecked()) options |= QTextDocument::FindBackward;
  41. if (ui->checkCase->isChecked()) options |= QTextDocument::FindCaseSensitively;
  42. if (ui->checkHigh->isChecked()) options |= QTextDocument::FindWholeWords;
  43. m_edit.findText(text, options);
  44. }
  45. //---------------------------------------------------------------------------
  46. // FbTextFindDlg
  47. //---------------------------------------------------------------------------
  48. FbTextFindDlg::FbTextFindDlg(FbTextEdit &edit)
  49. : QDialog(&edit)
  50. , ui(new Ui::FbFind)
  51. , m_edit(edit)
  52. {
  53. ui->setupUi(this);
  54. ui->checkHigh->hide();
  55. connect(ui->btnFind, SIGNAL(clicked()), this, SLOT(find()));
  56. }
  57. FbTextFindDlg::~FbTextFindDlg()
  58. {
  59. m_edit.findText(QString(), QWebPage::HighlightAllOccurrences);
  60. delete ui;
  61. }
  62. void FbTextFindDlg::find()
  63. {
  64. QString text = ui->editText->text();
  65. if (text.isEmpty()) return;
  66. QWebPage::FindFlags options = QWebPage::FindWrapsAroundDocument;
  67. if (ui->radioUp->isChecked()) options |= QWebPage::FindBackward;
  68. if (ui->checkCase->isChecked()) options |= QWebPage::FindCaseSensitively;
  69. m_edit.findText(text, options);
  70. options |= QWebPage::HighlightAllOccurrences;
  71. m_edit.findText(text, options);
  72. }
  73. //---------------------------------------------------------------------------
  74. // FbNoteDlg
  75. //---------------------------------------------------------------------------
  76. FbNoteDlg::FbNoteDlg(FbTextEdit &view)
  77. : QDialog(&view)
  78. {
  79. setWindowTitle(tr("Insert footnote"));
  80. resize(460, 300);
  81. QGridLayout * gridLayout = new QGridLayout(this);
  82. QLabel * label1 = new QLabel(this);
  83. label1->setText(tr("Identifier:"));
  84. gridLayout->addWidget(label1, 0, 0, 1, 1);
  85. m_key = new QComboBox(this);
  86. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  87. gridLayout->addWidget(m_key, 0, 1, 1, 1);
  88. QLabel * label2 = new QLabel(this);
  89. label2->setText(tr("Title:"));
  90. gridLayout->addWidget(label2, 1, 0, 1, 1);
  91. m_title = new QLineEdit(this);
  92. m_title->setObjectName(QString::fromUtf8("m_title"));
  93. gridLayout->addWidget(m_title, 1, 1, 1, 1);
  94. m_toolbar = new QToolBar(this);
  95. gridLayout->addWidget(m_toolbar, 2, 0, 1, 2);
  96. QFrame * frame = new QFrame(this);
  97. frame->setFrameShape(QFrame::StyledPanel);
  98. frame->setFrameShadow(QFrame::Sunken);
  99. gridLayout->addWidget(frame, 3, 0, 1, 2);
  100. QLayout * frameLayout = new QBoxLayout(QBoxLayout::LeftToRight, frame);
  101. frameLayout->setSpacing(0);
  102. frameLayout->setMargin(0);
  103. m_text = new FbTextBase(frame);
  104. m_text->setObjectName(QString::fromUtf8("m_text"));
  105. m_text->setUrl(QUrl(QString::fromUtf8("about:blank")));
  106. frameLayout->addWidget(m_text);
  107. QDialogButtonBox * buttonBox = new QDialogButtonBox(this);
  108. buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
  109. buttonBox->setOrientation(Qt::Horizontal);
  110. buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  111. gridLayout->addWidget(buttonBox, 4, 0, 1, 2);
  112. QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  113. QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  114. m_key->addItem(tr("<create new>"));
  115. m_key->setCurrentIndex(0);
  116. m_title->setFocus();
  117. FbTextPage *page = new FbTextPage(this);
  118. connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  119. page->setNetworkAccessManager(view.page()->networkAccessManager());
  120. page->setContentEditable(true);
  121. m_text->setPage(page);
  122. m_text->setHtml("<body><p><br></p></body>");
  123. m_text->addTools(m_toolbar);
  124. }
  125. void FbNoteDlg::loadFinished()
  126. {
  127. FbTextElement body = m_text->page()->mainFrame()->documentElement().findFirst("body");
  128. body.select();
  129. }
  130. //---------------------------------------------------------------------------
  131. // FbSetupDlg
  132. //---------------------------------------------------------------------------
  133. FbSetupDlg::FbSetupDlg(QWidget *parent)
  134. : QDialog(parent)
  135. , ui(new Ui::FbSetup)
  136. {
  137. ui->setupUi(this);
  138. }
  139. //---------------------------------------------------------------------------
  140. // FbImageDlg::FbTab
  141. //---------------------------------------------------------------------------
  142. FbImageDlg::FbTab::FbTab(QWidget* parent)
  143. : QWidget(parent)
  144. {
  145. QGridLayout * layout = new QGridLayout(this);
  146. label = new QLabel(this);
  147. label->setText(tr("File name:"));
  148. layout->addWidget(label, 0, 0, 1, 1);
  149. combo = new FbImageCombo(this);
  150. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  151. combo->setSizePolicy(sizePolicy);
  152. layout->addWidget(combo, 0, 1, 1, 1);
  153. QFrame *frame = new FbTextFrame(this);
  154. frame->setMinimumSize(QSize(300, 200));
  155. layout->addWidget(frame, 1, 0, 1, 2);
  156. preview = new QWebView(this);
  157. frame->layout()->addWidget(preview);
  158. }
  159. //---------------------------------------------------------------------------
  160. // FbImageCombo
  161. //---------------------------------------------------------------------------
  162. void FbImageCombo::showPopup()
  163. {
  164. QComboBox::showPopup();
  165. if (isEditable()) {
  166. emit popup();
  167. QComboBox::hidePopup();
  168. }
  169. }
  170. void FbImageCombo::selectFile()
  171. {
  172. QString filters;
  173. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif)") += ";;";
  174. filters += tr("Portable Network Graphics (PNG) (*.png)") += ";;";
  175. filters += tr("JPEG (*.jpg *.jpeg)") += ";;";
  176. filters += tr("Graphics Interchange Format (*.gif)") += ";;";
  177. filters += tr("All Files (*)");
  178. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  179. if (!path.isEmpty()) setEditText(path);
  180. }
  181. //---------------------------------------------------------------------------
  182. // FbImageDlg
  183. //---------------------------------------------------------------------------
  184. FbImageDlg::FbImageDlg(FbTextEdit *text)
  185. : QDialog(text)
  186. , tabFile(0)
  187. , tabPict(0)
  188. {
  189. setWindowTitle(tr("Insert picture"));
  190. QBoxLayout *layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
  191. notebook = new QTabWidget(this);
  192. layout->addWidget(notebook);
  193. QDialogButtonBox *buttons = new QDialogButtonBox(this);
  194. buttons->setOrientation(Qt::Horizontal);
  195. buttons->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  196. layout->addWidget(buttons);
  197. connect(buttons, SIGNAL(accepted()), SLOT(accept()));
  198. connect(buttons, SIGNAL(rejected()), SLOT(reject()));
  199. connect(notebook, SIGNAL(currentChanged(int)), SLOT(notebookChanged(int)));
  200. QUrl url = text->url();
  201. tabFile = new FbTab(notebook);
  202. tabFile->combo->setEditable(true);
  203. tabFile->preview->setHtml(QString(), url);
  204. connect(tabFile->combo, SIGNAL(popup()), tabFile->combo, SLOT(selectFile()));
  205. notebook->addTab(tabFile, tr("Select file"));
  206. if (text->store()->count()) {
  207. FbListModel *model = new FbListModel(text, this);
  208. tabPict = new FbTab(notebook);
  209. tabPict->preview->setHtml(QString(), url);
  210. tabPict->combo->setModel(model);
  211. tabPict->combo->setCurrentIndex(0);
  212. tabPict->preview->page()->setNetworkAccessManager(text->page()->networkAccessManager());
  213. notebook->addTab(tabPict, tr("From collection"));
  214. connect(tabPict->combo, SIGNAL(activated(QString)), SLOT(pictureActivated(QString)));
  215. tabPict->combo->setFocus();
  216. }
  217. QString style =
  218. // "QComboBox::drop-down{border:0px;margin:0px;}"
  219. "QComboBox::down-arrow{image:url(:dots.png);}";
  220. tabFile->combo->setStyleSheet(style);
  221. tabFile->combo->setFocus();
  222. resize(minimumSizeHint());
  223. }
  224. void FbImageDlg::notebookChanged(int index)
  225. {
  226. if (index) {
  227. disconnect(notebook, SIGNAL(currentChanged(int)), this, SLOT(notebookChanged(int)));
  228. if (tabPict) pictureActivated(tabPict->combo->itemText(0));
  229. }
  230. }
  231. void FbImageDlg::pictureActivated(const QString & text)
  232. {
  233. QUrl url = tabPict->preview->url();
  234. url.setFragment(text);
  235. QString html = QString("<img src=%1 valign=center align=center width=100%>").arg(url.toString());
  236. tabPict->preview->setHtml(html, tabPict->preview->url());
  237. }