text_controller.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: text_controller.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-plaintext.
00007  *
00008  * uds-plugin-plaintext is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-plaintext is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include "log.h"
00028 #include "text_tasks.h"
00029 #include "text_view.h"
00030 #include "text_controller.h"
00031 
00032 namespace text
00033 {
00034 
00035 TextController::TextController(TextModel *_model, TextView *_view)
00036 : model(_model)
00037 , view(_view)
00038 , pagination_paused(false)
00039 {
00040     // Set controller for view.
00041     view->set_controller(this);
00042 }
00043 
00044 TextController::~TextController()
00045 {
00046 }
00047 
00048 void TextController::start()
00049 {
00050     // Launch worker thread.
00051     thread.start();
00052 }
00053 
00054 void TextController::stop()
00055 {
00056     // Stop worker thread, wait until all tasks done.
00057     thread.stop();
00058 }
00059 
00060 void TextController::render(unsigned int id, unsigned int page_index, void *data, unsigned char* bmp)
00061 {
00062     Position start_pos;
00063     if (view->get_anchor_by_page(page_index, start_pos))
00064     {
00065         render(id, start_pos, data, bmp);
00066     }
00067     else
00068     {
00069         ERRORPRINTF("Can't render page %d because it does not exist.\n", page_index);
00070     }
00071 }
00072 
00073 void TextController::render(unsigned int id, const Position& start_pos, void *data, unsigned char* bmp)
00074 {
00075     LOGPRINTF("About to launch render task.");
00076     view->check_page_table(start_pos);
00077     thread.prepend_task(new RenderTask(id, view, start_pos, data, bmp), true);
00078 }
00079 
00080 void TextController::paginate(const Position& start, bool send_start_notification)
00081 {
00082     pagination_pending = true;
00083     PaginationTask *task = new PaginationTask(view, start, !send_start_notification);
00084     task->abort_signal.add_slot(this, &TextController::on_pagination_aborted);
00085     view->pagination_end_signal.add_slot(this, &TextController::on_pagination_finished);
00086     thread.append_task(task);
00087 }
00088 
00089 void TextController::on_pagination_finished(unsigned int, unsigned int)
00090 {
00091     if (!pagination_paused)
00092     {
00093         pagination_pending = false;
00094     }
00095 }
00096 
00097 void TextController::on_pagination_aborted(const Position& start)
00098 {
00099     // If repagination is needed, we don't need to launch a same
00100     // pagination again. A new pagination task will be launched
00101     // by render task soon.
00102     //
00103     // Also when pagination is paused, we don't want to restart
00104     // the pagination immediately
00105     //
00106     if ((!pagination_paused) && (!view->need_repagination()))
00107     {
00108         paginate(start, false /* don't send pagination start notification */);
00109     }
00110 }
00111 
00112 void TextController::pause_pagination()
00113 {
00114     TRACEFUNCTION();
00115 
00116     if (pagination_pending)
00117     {
00118         pagination_paused = true;
00119         
00120         unsigned int total_pages = view->get_page_count();
00121         unsigned int current_page = view->get_current_page_index();
00122         
00123         // Reporting pagination done to UDS through our internal pagination end signal
00124         // UDS probably doesn't receive this event because the document window is
00125         // about to be deactivated (hence, UDS will unsubscribe from plugin events),
00126         // however I still think it is good practice to try to notify UDS anyway.
00127         // [MdB]
00128         view->pagination_end_signal.broadcast(current_page, total_pages);  
00129     
00130         PaginationAbortTask *task = new PaginationAbortTask(view);
00131         thread.prepend_task(task, true /* Cancel running task */);
00132     }
00133 }
00134 
00135 void TextController::restart_pagination()
00136 {
00137      TRACEFUNCTION();
00138 
00139    
00140     // Only restart pagination if there is a need
00141     if (pagination_pending)
00142     {
00143         pagination_paused = false;
00144 
00145         Position start_pos;
00146         view->check_page_table(start_pos);
00147         paginate(start_pos, false /* don't send pagination start notification */);
00148     }
00149 }
00150 
00151 
00152 void TextController::search(SearchContext* search_context)
00153 {
00154     SearchTask* task = new SearchTask(model, search_context);
00155     task->abort_signal.add_slot(this, &TextController::on_search_aborted);
00156     thread.prepend_task(task, true);
00157 }
00158 
00159 void TextController::on_search_aborted(SearchContext* sc)
00160 {
00161     SearchTask* task = new SearchTask(model, sc);
00162     task->abort_signal.add_slot(this, &TextController::on_search_aborted);
00163     thread.prepend_task(task, false);
00164 }
00165 
00166 }
00167 
Generated by  doxygen 1.6.2-20100208