1 /************************************************************************ 2 ** 3 ** Copyright (C) 2015-2020 Kevin B. Hendricks, Stratford, Ontario Canada 4 ** Copyright (C) 2009-2011 Strahinja Markovic <strahinja.markovic@gmail.com> 5 ** 6 ** This file is part of Sigil. 7 ** 8 ** Sigil is free software: you can redistribute it and/or modify 9 ** it under the terms of the GNU General Public License as published by 10 ** the Free Software Foundation, either version 3 of the License, or 11 ** (at your option) any later version. 12 ** 13 ** Sigil is distributed in the hope that it will be useful, 14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ** GNU General Public License for more details. 17 ** 18 ** You should have received a copy of the GNU General Public License 19 ** along with Sigil. If not, see <http://www.gnu.org/licenses/>. 20 ** 21 *************************************************************************/ 22 23 #pragma once 24 #ifndef CONTENTTAB_H 25 #define CONTENTTAB_H 26 27 #include <QWidget> 28 #include <QString> 29 #include <QtCore/QUrl> 30 31 #include "Misc/Utility.h" 32 #include "ViewEditors/Zoomable.h" 33 34 class QLayout; 35 class Searchable; 36 class Resource; 37 38 /** 39 * A generic tab widget for editing/viewing a resource. 40 */ 41 class ContentTab : public QWidget, public Zoomable 42 { 43 Q_OBJECT 44 45 public: 46 47 /** 48 * Constructor. 49 * 50 * @param resource The resource this tab will be displaying. 51 * @param parent The parent of this QObject. 52 */ 53 ContentTab(Resource *resource, QWidget *parent = 0); 54 55 /** 56 * Destructor. 57 */ 58 virtual ~ContentTab(); 59 60 /** 61 * Returns the filename of the displayed resource. 62 * 63 * @return The filename of the displayed resource. 64 */ 65 QString GetFilename(); 66 67 QString GetShortPathName(); 68 69 /** 70 * Returns the icon appropriate for the displayed resource. 71 * 72 * @return The icon appropriate for the displayed resource. 73 */ 74 QIcon GetIcon(); 75 76 /** 77 * The resource currently loaded in the tab. 78 * 79 * @return A reference to the resource. 80 */ 81 Resource *GetLoadedResource(); 82 83 /** 84 * The modification state of the resource. 85 * 86 * @return \c true if the tab has modified the resource. 87 */ IsModified()88 virtual bool IsModified() { 89 return false; 90 } 91 92 /** 93 * Can the user perform the Cut clipboard operation. 94 * 95 * @return \c true if the user can Cut. 96 */ CutEnabled()97 virtual bool CutEnabled() { 98 return false; 99 } 100 101 /** 102 * Can the user perform the Copy clipboard operation. 103 * 104 * @return \c true if the user can Copy. 105 */ CopyEnabled()106 virtual bool CopyEnabled() { 107 return false; 108 } 109 110 /** 111 * Can the user perform the Paste clipboard operation. 112 * 113 * @return \c true if the user can Paste. 114 */ PasteEnabled()115 virtual bool PasteEnabled() { 116 return false; 117 } 118 DeleteLineEnabled()119 virtual bool DeleteLineEnabled() { 120 return false; 121 } 122 123 /* 124 * Can the user perform the Cut Code Tags operation. 125 * 126 * @return \c true if the user can Cut. 127 */ RemoveFormattingEnabled()128 virtual bool RemoveFormattingEnabled() { 129 return false; 130 } 131 132 /* 133 * Can the user perform the Cut Tag Pair operation. 134 * 135 * @return \c true if the user can Cut. 136 */ RemoveTagPairEnabled()137 virtual bool RemoveTagPairEnabled() { 138 return false; 139 } 140 141 /* 142 * Can the user perform the Cut Code Tags operation. 143 * 144 * @return \c true if the user can Cut. 145 */ InsertClosingTagEnabled()146 virtual bool InsertClosingTagEnabled() { 147 return false; 148 } 149 AddToIndexEnabled()150 virtual bool AddToIndexEnabled() { 151 return false; 152 } MarkForIndexEnabled()153 virtual bool MarkForIndexEnabled() { 154 return false; 155 } 156 InsertIdEnabled()157 virtual bool InsertIdEnabled() { 158 return false; 159 } InsertHyperlinkEnabled()160 virtual bool InsertHyperlinkEnabled() { 161 return false; 162 } InsertSpecialCharacterEnabled()163 virtual bool InsertSpecialCharacterEnabled() { 164 return false; 165 } InsertFileEnabled()166 virtual bool InsertFileEnabled() { 167 return false; 168 } ToggleAutoSpellcheckEnabled()169 virtual bool ToggleAutoSpellcheckEnabled() { 170 return false; 171 } 172 173 /** 174 * Checked state of the CodeView action. 175 * 176 * @return \c true if the CodeView action should be checked. 177 */ CodeViewChecked()178 virtual bool CodeViewChecked() { 179 return false; 180 } 181 GetCaretLocationUpdate()182 virtual QString GetCaretLocationUpdate() const { 183 return QString(); 184 } 185 GetCursorPosition()186 virtual int GetCursorPosition() const { 187 return 0; 188 } GetCursorLine()189 virtual int GetCursorLine() const { 190 return 0; 191 } GetCursorColumn()192 virtual int GetCursorColumn() const { 193 return 0; 194 } 195 GetZoomFactor()196 virtual float GetZoomFactor() const { 197 return 1.0; 198 } SetZoomFactor(float new_zoom_factor)199 virtual void SetZoomFactor(float new_zoom_factor) { } 200 201 /** 202 * Update content displayed in the tab. 203 * 204 * For example after a zoom operation. 205 */ UpdateDisplay()206 virtual void UpdateDisplay() {} 207 208 /** 209 * Returns a pointer to searchable content in the tab. 210 * Returns pointer instead of reference because we need 211 * to return NULL for tabs with no searchable content. 212 * 213 * @return The searchable content. 214 */ 215 virtual Searchable *GetSearchableContent(); 216 IsLoadingFinished()217 virtual bool IsLoadingFinished() { 218 return true; 219 } 220 221 /** 222 * Checked state of the Bold action. 223 * 224 * @return \c true if the Bold action should be checked. 225 */ BoldChecked()226 virtual bool BoldChecked() { 227 return false; 228 } 229 230 /** 231 * Checked state of the Italic action. 232 * 233 * @return \c true if the Italic action should be checked. 234 */ ItalicChecked()235 virtual bool ItalicChecked() { 236 return false; 237 } 238 239 /** 240 * Checked state of the Underline action. 241 * 242 * @return \c true if the Underline action should be checked. 243 */ UnderlineChecked()244 virtual bool UnderlineChecked() { 245 return false; 246 } 247 248 /** 249 * Checked state of the Strikethrough action. 250 * 251 * @return \c true if the Strikethrough action should be checked. 252 */ StrikethroughChecked()253 virtual bool StrikethroughChecked() { 254 return false; 255 } 256 257 /** 258 * Checked state of the Subscrip action. 259 * 260 * @return \c true if the Subscrip action should be checked. 261 */ SubscriptChecked()262 virtual bool SubscriptChecked() { 263 return false; 264 } 265 266 /** 267 * Checked state of the Superscript action. 268 * 269 * @return \c true if the Superscript action should be checked. 270 */ SuperscriptChecked()271 virtual bool SuperscriptChecked() { 272 return false; 273 } 274 AlignLeftChecked()275 virtual bool AlignLeftChecked() { 276 return false; 277 } AlignRightChecked()278 virtual bool AlignRightChecked() { 279 return false; 280 } AlignCenterChecked()281 virtual bool AlignCenterChecked() { 282 return false; 283 } AlignJustifyChecked()284 virtual bool AlignJustifyChecked() { 285 return false; 286 } 287 288 /** 289 * Checked state of the BulletList action. 290 * 291 * @return \c true if the BulletList action should be checked. 292 */ BulletListChecked()293 virtual bool BulletListChecked() { 294 return false; 295 } 296 297 /** 298 * Checked state of the NumberList action. 299 * 300 * @return \c true if the NumberList action should be checked. 301 */ NumberListChecked()302 virtual bool NumberListChecked() { 303 return false; 304 } 305 306 /** 307 * Returns the name of the element the caret is located in. 308 * 309 * @return The name of the element the caret is located in. 310 */ GetCaretElementName()311 virtual QString GetCaretElementName() { 312 return ""; 313 } 314 MarkSelection()315 virtual bool MarkSelection() { 316 return false; 317 } 318 ClearMarkedText()319 virtual bool ClearMarkedText() { 320 return false; 321 } 322 323 324 public slots: 325 326 /** 327 * Saves the tab data, then schedules tab for deletion. 328 */ 329 void Close(); 330 331 /** 332 * Saves the changed content when the user leaves the tab. 333 */ 334 virtual void SaveTabContent(); 335 336 /** 337 * Loads the resource content when the user enters the tab. 338 */ 339 virtual void LoadTabContent(); 340 341 /** 342 * Emits the CentralTabRequest signal. 343 */ 344 void EmitCentralTabRequest(); 345 346 /** 347 * Refresh display after external change of contents 348 * (Replace in All Files). 349 */ 350 void ContentChangedExternally(); 351 352 /** 353 * Change casing of the selected text. 354 */ 355 virtual void ChangeCasing(const Utility::Casing casing); 356 357 /** 358 * Returns state of underlying resource 359 */ 360 bool GetResourceWasDeleted(); 361 362 signals: 363 364 /** 365 * Emitted when the tab wants to be deleted. 366 * 367 * @param tab_to_delete A pointer to this tab. 368 */ 369 void DeleteMe(ContentTab *tab_to_delete); 370 371 /** 372 * Emitted when the zoom factor changes. 373 * 374 * @param factor The new zoom factor. 375 */ 376 void ZoomFactorChanged(float factor); 377 378 /** 379 * Emitted when the content of the tab changes. 380 */ 381 void ContentChanged(); 382 383 /** 384 * Emitted when tab header text has been changed. 385 * 386 * @param renamed_tab Pointer to this tab. 387 */ 388 void TabRenamed(ContentTab *renamed_tab); 389 390 /** 391 * Emitted when the tab wants to become the 392 * central (shown) tab of the UI. 393 * 394 * @param tab The tab that wants to become central. 395 */ 396 void CentralTabRequest(ContentTab *tab); 397 398 /** 399 * Emitted when the cursor position changes. 400 * 401 * Not all tabs have a cursor so this won't be emitted by 402 * all tabs. 403 * 404 * @param line The line the cursor is at. 405 * @param column The column the cursor is at. 406 */ 407 void UpdateCursorPosition(int line, int column); 408 409 /** 410 * Emitted when we want to do some operations with the clipboard 411 * to paste things into elsewhere, but restoring state afterwards 412 * so that Clipboard History and current clipboard contents are 413 * left unaffected. 414 */ 415 void ClipboardSaveRequest(); 416 void ClipboardRestoreRequest(); 417 418 void SpellingHighlightRefreshRequest(); 419 420 void InsertedFileOpenedExternally(const QString &pathname); 421 422 void InsertedFileSaveAs(const QUrl &url); 423 424 void ShowStatusMessageRequest(const QString &message); 425 426 void MarkSelectionRequest(); 427 void ClearMarkedTextRequest(); 428 429 protected slots: 430 431 /** 432 * Invoked when underlying resource sends out its Deleted signal 433 */ 434 void UnderlyingResourceDeleted(); 435 436 /** 437 * Emits the DeleteMe signal. 438 */ 439 void EmitDeleteMe(); 440 441 /** 442 * Emits the TabRenamed signal. 443 */ 444 void EmitTabRenamed(); 445 446 protected: 447 448 /** 449 * A custom focusIn handler. 450 * By default, calls the LoadTabContent function. 451 * 452 * @warning Will \b not be called for child tabs 453 * that set a focus proxy (like FlowTab, TextTab etc.) 454 */ 455 virtual void focusInEvent(QFocusEvent *event); 456 457 /** 458 * A custom focusOut handler. 459 * By default, calls the SaveTabContent function. 460 * 461 * @warning Will \b not be called for child tabs 462 * that set a focus proxy (like FlowTab, TextTab etc.) 463 */ 464 virtual void focusOutEvent(QFocusEvent *event); 465 466 467 /////////////////////////////// 468 // PROTECTED MEMBER VARIABLES 469 /////////////////////////////// 470 471 /** 472 * The resource being displayed. 473 */ 474 Resource *m_Resource; 475 476 /** 477 * The main layout of the widget. 478 */ 479 QLayout *m_Layout; 480 481 /** 482 * Has our underlying resource been deleted 483 */ 484 bool m_resource_was_deleted; 485 486 }; 487 488 #endif // CONTENTTAB_H 489 490 491