text_tasks.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: text_tasks.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 "plugin_render_result.h"
00028 
00029 #include "log.h"
00030 #include "text_tasks.h"
00031 #include "text_view.h"
00032 
00033 namespace text
00034 {
00035 
00036 InitializationTask::InitializationTask(TextView *view)
00037 : text_view(view)
00038 {
00039 }
00040 
00041 InitializationTask::~InitializationTask()
00042 {
00043 }
00044 
00045 void InitializationTask::execute()
00046 {
00047     text_view->initialize();
00048 }
00049 
00050 DeinitializationTask::DeinitializationTask(TextView *view)
00051 : text_view(view)
00052 {
00053 }
00054 
00055 DeinitializationTask::~DeinitializationTask()
00056 {
00057 }
00058 
00059 void DeinitializationTask::execute()
00060 {
00061     text_view->deinitialize();
00062 }
00063 
00064 PaginationTask::PaginationTask(TextView *view, const Position& _start, bool _is_child)
00065 : text_view(view), start_pos(_start), is_child(_is_child)
00066 {
00067 }
00068 
00069 PaginationTask::~PaginationTask()
00070 {
00071 }
00072 
00073 void PaginationTask::execute()
00074 {
00075     if (!is_child)
00076     {
00077         // Tell listener that pagination starts.
00078         LOGPRINTF("About to send pagination start signal.");
00079         text_view->pagination_start_signal.broadcast(1);
00080     }
00081 
00082     bool pagination_done = false;
00083     while (!is_aborted())
00084     {
00085         if (text_view->paginate(start_pos))
00086         {
00087             // Pagination is complete.
00088             pagination_done = true;
00089             break;
00090         }
00091     }
00092 
00093     if (pagination_done)
00094     {
00095         // Tell listener that pagination is done.
00096         unsigned int total_pages = text_view->get_page_count();
00097         unsigned int current_page = text_view->get_current_page_index();
00098         LOGPRINTF("About to send pagination done signal.");
00099         text_view->pagination_end_signal.broadcast(current_page, total_pages);
00100     }
00101     else
00102     {
00103         // We are aborted.
00104         LOGPRINTF("Pagination aborted...");
00105         abort_signal.broadcast(start_pos);
00106     }
00107 }
00108 
00109 
00110 PaginationAbortTask::PaginationAbortTask(TextView *view)
00111 : text_view(view)
00112 {
00113     TRACEFUNCTION();
00114 }
00115 
00116 PaginationAbortTask::~PaginationAbortTask()
00117 {
00118     TRACEFUNCTION();
00119 }
00120 
00121 void PaginationAbortTask::execute()
00122 {
00123     TRACEFUNCTION();
00124 
00125     //
00126     // Dummy task to abort pagination
00127     //
00128     
00129     // Example usage:
00130     //   PaginationAbortTask *task = new PaginationAbortTask();
00131     //   thread.prepend_task(task, true /* Cancel running task */);
00132 }
00133 
00134 RenderTask::RenderTask(unsigned int     _id,
00135                        TextView        *_view,
00136                        const Position&  _pos,
00137                        void            *_user_data,
00138                        unsigned char   *_bmp)
00139 : id(_id)
00140 , text_view(_view)
00141 , start_pos(_pos)
00142 , user_data(_user_data)
00143 , bmp(_bmp)
00144 {
00145 }
00146 
00147 RenderTask::~RenderTask()
00148 {
00149 }
00150 
00151 void RenderTask::execute()
00152 {
00153     Position end_pos = text_view->render(bmp, start_pos);
00154 
00155     // Tell listener that render done.
00156     text_view->render_done_signal.broadcast(id, start_pos, end_pos, user_data);
00157 }
00158 
00159 SearchTask::SearchTask(TextModel *_model, SearchContext* _sc)
00160 : text_model(_model), sc(_sc)
00161 {
00162 }
00163 
00164 SearchTask::~SearchTask()
00165 {
00166 }
00167 
00168 void SearchTask::execute()
00169 {
00170     std::vector<Range> result_ranges;
00171     bool search_done = false;
00172     while (!is_aborted() &&
00173            text_model->get_aborting_search_task_id() != sc->search_id)
00174     {
00175         if (text_model->search(result_ranges, sc))
00176         {
00177             // We reached the start/end of the document, 
00178             // or we find an occurrence if the SearchType is SEARCH_NEXT.
00179             search_done = true;
00180             break;
00181         }
00182     }
00183 
00184     if (is_aborted())
00185     {
00186         LOGPRINTF("Search task is aborted by another task...");
00187         abort_signal.broadcast(sc);
00188     }
00189 
00190     if (search_done)
00191     {
00192         // Tell listeners search task is done.
00193         text_model->search_done_signal.broadcast(result_ranges, sc);
00194     }
00195     else
00196     {
00197         // We are aborted, do nothing.
00198         LOGPRINTF("Search task is aborted by user...");
00199     }
00200 }
00201 
00202 }
00203 
Generated by  doxygen 1.6.2-20100208